Skip to content

Commit 3ecd038

Browse files
authored
Fixing missing base class in reflection docs, adding to reflection test (#1283)
1 parent 91d3932 commit 3ecd038

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

docs/models.rst

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,14 +139,19 @@ defining the full model.
139139

140140
Call the :meth:`~.SQLAlchemy.reflect` method on the extension. It will reflect all the
141141
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.
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

tests/test_metadata.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff 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

0 commit comments

Comments
 (0)