-
Notifications
You must be signed in to change notification settings - Fork 9
Add max depth parameter to be able load nested relationships on a model #44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
6d78778
775190f
222cf6c
77b93c5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,6 +36,21 @@ class Model(ModelBase): | |
| pass | ||
|
|
||
|
|
||
| class Book(Model): | ||
| __tablename__ = "books" | ||
|
|
||
| id: Mapped[int] = mapped_column(sa.Integer(), primary_key=True) | ||
| title: Mapped[str] = mapped_column(sa.String()) | ||
| created_by: Mapped[int] = mapped_column(sa.Integer(), sa.ForeignKey("users.id"), nullable=False) | ||
| created_by_user: Mapped["User"] = relationship( | ||
| "User", foreign_keys=[created_by], back_populates="created_books" | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For my own knowledge since I never really used |
||
| ) | ||
| updated_by: Mapped[int] = mapped_column(sa.Integer(), sa.ForeignKey("users.id"), nullable=False) | ||
| updated_by_user: Mapped["User"] = relationship( | ||
| "User", foreign_keys=[updated_by], back_populates="updated_books" | ||
| ) | ||
|
|
||
|
|
||
| class User(Model): | ||
| __tablename__ = "users" | ||
|
|
||
|
|
@@ -48,6 +63,12 @@ class User(Model): | |
| "GroupMembership", back_populates="user" | ||
| ) | ||
| items: Mapped[t.List["GroupMembership"]] = relationship("Item") | ||
| created_books: Mapped[t.List["Book"]] = relationship( | ||
| "Book", foreign_keys=[Book.created_by], back_populates="created_by_user" | ||
| ) | ||
| updated_books: Mapped[t.List["Book"]] = relationship( | ||
| "Book", foreign_keys=[Book.updated_by], back_populates="updated_by_user" | ||
| ) | ||
|
|
||
|
|
||
| class Address(Model): | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,7 +9,7 @@ | |
|
|
||
| from sqlservice import Database, ModelBase, ModelMeta, as_declarative, declarative_base | ||
|
|
||
| from .fixtures import Address, Group, GroupMembership, Item, Note, User | ||
| from .fixtures import Address, Book, Group, GroupMembership, Item, Note, User | ||
|
|
||
|
|
||
| parametrize = pytest.mark.parametrize | ||
|
|
@@ -217,6 +217,8 @@ def test_model_to_dict__with_non_persisted_model(model: ModelBase, args: dict, e | |
| "addresses": [{"id": 1, "user_id": 1, "addr": "a", "zip_code": "1"}], | ||
| "group_memberships": [], | ||
| "items": [], | ||
| "created_books": [], | ||
| "updated_books": [], | ||
| }, | ||
| id="1:M_all_loaded_include_relationships", | ||
| ), | ||
|
|
@@ -245,7 +247,7 @@ def test_model_to_dict__with_non_persisted_model(model: ModelBase, args: dict, e | |
| GroupMembership(group=Group(name="g2")), | ||
| ], | ||
| ), | ||
| {}, | ||
| {"include_nested_relationships": True}, | ||
| [orm.joinedload("*")], | ||
| { | ||
| "id": 1, | ||
|
|
@@ -260,6 +262,8 @@ def test_model_to_dict__with_non_persisted_model(model: ModelBase, args: dict, e | |
| {"group_id": 2, "user_id": 1, "group": {"id": 2, "name": "g2"}}, | ||
| ], | ||
| "items": [], | ||
| "created_books": [], | ||
| "updated_books": [], | ||
| }, | ||
| id="nested_relationships_all_loaded", | ||
| ), | ||
|
|
@@ -282,7 +286,7 @@ def test_model_to_dict__with_non_persisted_model(model: ModelBase, args: dict, e | |
| ) | ||
| ], | ||
| ), | ||
| {"lazyload": True}, | ||
| {"lazyload": True, "include_nested_relationships": True}, | ||
| [orm.lazyload("*")], | ||
| { | ||
| "id": 1, | ||
|
|
@@ -323,9 +327,103 @@ def test_model_to_dict__with_non_persisted_model(model: ModelBase, args: dict, e | |
| }, | ||
| } | ||
| ], | ||
| "created_books": [], | ||
| "updated_books": [], | ||
| }, | ||
| id="nested_relationships_lazy_loaded", | ||
| ), | ||
| param( | ||
| Book( | ||
| id=1, | ||
| title="t", | ||
| created_by_user=User(id=1, name="n"), | ||
| updated_by_user=User(id=2, name="n"), | ||
| ), | ||
| {"lazyload": True}, | ||
| [], | ||
| { | ||
| "id": 1, | ||
| "title": "t", | ||
| "created_by": 1, | ||
| "updated_by": 2, | ||
| "created_by_user": {"id": 1, "name": "n", "active": True}, | ||
| "updated_by_user": {"id": 2, "name": "n", "active": True}, | ||
| }, | ||
| id="exclude_nested_relationships_lazy_loaded_with_circular_reference", | ||
| ), | ||
| param( | ||
| Book( | ||
| id=1, | ||
| title="t", | ||
| created_by_user=User(id=1, name="n"), | ||
| updated_by_user=User(id=2, name="n"), | ||
| ), | ||
| {}, | ||
| [], | ||
| { | ||
| "id": 1, | ||
| "title": "t", | ||
| "created_by": 1, | ||
| "updated_by": 2, | ||
| }, | ||
| id="exclude_nested_relationships_with_circular_reference", | ||
| ), | ||
| param( | ||
| Book( | ||
| id=1, | ||
| title="t", | ||
| created_by_user=User(id=1, name="n"), | ||
| updated_by_user=User(id=2, name="n"), | ||
| ), | ||
| {"lazyload": True, "include_nested_relationships": True}, | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would be good to also have tests without lazy-loading that verify that non-loaded nested relationships won't be included. |
||
| [], | ||
| { | ||
| "id": 1, | ||
| "title": "t", | ||
| "created_by": 1, | ||
| "updated_by": 2, | ||
| "created_by_user": { | ||
| "id": 1, | ||
| "name": "n", | ||
| "active": True, | ||
| "addresses": [], | ||
| "group_memberships": [], | ||
| "items": [], | ||
| "created_books": [{"id": 1, "title": "t", "created_by": 1, "updated_by": 2}], | ||
| "updated_books": [], | ||
| }, | ||
| "updated_by_user": { | ||
| "id": 2, | ||
| "name": "n", | ||
| "active": True, | ||
| "addresses": [], | ||
| "group_memberships": [], | ||
| "items": [], | ||
| "created_books": [], | ||
| "updated_books": [ | ||
| { | ||
| "id": 1, | ||
| "title": "t", | ||
| "created_by": 1, | ||
| "updated_by": 2, | ||
| "created_by_user": { | ||
| "id": 1, | ||
| "name": "n", | ||
| "active": True, | ||
| "addresses": [], | ||
| "group_memberships": [], | ||
| "items": [], | ||
| "created_books": [ | ||
| {"id": 1, "title": "t", "created_by": 1, "updated_by": 2} | ||
| ], | ||
| "updated_books": [], | ||
| }, | ||
| } | ||
| ], | ||
| }, | ||
| }, | ||
| id="include_nested_relationships_lazy_loaded_with_circular_reference", | ||
| ), | ||
| ], | ||
| ) | ||
| def test_model_to_dict__loaded_from_database( | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Group relationship arguments together: