File tree Expand file tree Collapse file tree 2 files changed +21
-2
lines changed Expand file tree Collapse file tree 2 files changed +21
-2
lines changed Original file line number Diff line number Diff line change @@ -139,14 +139,19 @@ defining the full model.
139139
140140Call the :meth: `~.SQLAlchemy.reflect ` method on the extension. It will reflect all the
141141tables for each bind key. Each metadata's ``tables `` attribute will contain the detected
142- table objects.
142+ table objects. See :doc: ` binds ` for more details on bind keys.
143143
144144.. code-block :: python
145145
146146 with app.app_context():
147147 db.reflect()
148148
149- class User :
149+ # From the default bind key
150+ class Book (db .Model ):
151+ __table__ = db.metadata.tables[" book" ]
152+
153+ # From an "auth" bind key
154+ class User (db .Model ):
150155 __table__ = db.metadatas[" auth" ].tables[" user" ]
151156
152157 In most cases, it will be more maintainable to define the model classes yourself. You
Original file line number Diff line number Diff line change @@ -162,3 +162,17 @@ def test_reflect(app: Flask) -> None:
162162 db .reflect ()
163163 assert "user" in db .metadata .tables
164164 assert "post" in db .metadatas ["post" ].tables
165+
166+ class User (db .Model ):
167+ __table__ = db .metadata .tables ["user" ]
168+
169+ class Post (db .Model ):
170+ __table__ = db .metadatas ["post" ].tables ["post" ]
171+
172+ db .session .add (User (id = 1 ))
173+ users = db .session .execute (sa .select (User )).scalars ().all ()
174+ assert len (users ) == 1
175+
176+ db .session .add (Post (id = 1 ))
177+ posts = db .session .execute (sa .select (Post )).scalars ().all ()
178+ assert len (posts ) == 1
You can’t perform that action at this time.
0 commit comments