Skip to content

Commit 24c1713

Browse files
committed
Improved documentation for model registration
1 parent 9ae4eba commit 24c1713

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

docs/patterns/sqlalchemy.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ Here the example `database.py` module for your application::
4747
Base.query = db_session.query_property()
4848

4949
def init_db():
50+
# import all modules here that might define models so that
51+
# they will be registered properly on the metadata. Otherwise
52+
# you will have to import them first before calling init_db()
53+
import yourapplication.models
5054
Base.metadata.create_all(bind=engine)
5155

5256
To define your models, just subclass the `Base` class that was created by
@@ -84,6 +88,11 @@ Here is an example model (put this into `models.py`, e.g.)::
8488
def __repr__(self):
8589
return '<User %r>' % (self.name)
8690

91+
To create the database you can use the `init_db` function:
92+
93+
>>> from yourapplication.database import init_db
94+
>>> init_db()
95+
8796
You can insert entries into the database like this:
8897

8998
>>> from yourapplication.database import db_session

0 commit comments

Comments
 (0)