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.
139
139
140
140
Call the :meth: `~.SQLAlchemy.reflect ` method on the extension. It will reflect all the
141
141
tables 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.
143
143
144
144
.. code-block :: python
145
145
146
146
with app.app_context():
147
147
db.reflect()
148
148
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 ):
150
155
__table__ = db.metadatas[" auth" ].tables[" user" ]
151
156
152
157
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:
162
162
db .reflect ()
163
163
assert "user" in db .metadata .tables
164
164
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