-
-
Notifications
You must be signed in to change notification settings - Fork 768
✨ Support SQLAlchemy keyed dict models #1287
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
Open
nkrishnaswami
wants to merge
12
commits into
fastapi:main
Choose a base branch
from
nkrishnaswami:allow-dict-relationship
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+48
−0
Open
Changes from 9 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
6f629d7
Minimal change to allow `attribute_keyed_dict` + test
nkrishnaswami 7f1d085
Remove `StrEnum`
nkrishnaswami ad7f6bb
Remove type union pipe syntax
nkrishnaswami 563886a
Use `Dict[]` instead of `dict[]`
429990f
Merge branch 'main' into allow-dict-relationship
svlandeg f684896
Merge branch 'main' into allow-dict-relationship
svlandeg b9616fe
Merge branch 'main' into allow-dict-relationship
nkrishnaswami 81fed58
Merge branch 'main' into allow-dict-relationship
nkrishnaswami 228c087
Merge branch 'main' into allow-dict-relationship
svlandeg fb43fd4
Remove superfluous index in test case
nkrishnaswami 8a693f0
🎨 [pre-commit.ci] Auto format from pre-commit.com hooks
pre-commit-ci[bot] a5c3e8f
Merge branch 'main' into allow-dict-relationship
nkrishnaswami File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
from enum import Enum | ||
from typing import Dict, Optional | ||
|
||
from sqlalchemy.orm.collections import attribute_keyed_dict | ||
from sqlmodel import Field, Index, Relationship, Session, SQLModel, create_engine | ||
|
||
|
||
def test_attribute_keyed_dict_works(clear_sqlmodel): | ||
class Color(str, Enum): | ||
Orange = "Orange" | ||
Blue = "Blue" | ||
|
||
class Child(SQLModel, table=True): | ||
__tablename__ = "children" | ||
__table_args__ = ( | ||
Index("ix_children_parent_id_color", "parent_id", "color", unique=True), | ||
) | ||
|
||
id: Optional[int] = Field(primary_key=True, default=None) | ||
parent_id: int = Field(foreign_key="parents.id") | ||
color: Color | ||
value: int | ||
|
||
class Parent(SQLModel, table=True): | ||
__tablename__ = "parents" | ||
|
||
id: Optional[int] = Field(primary_key=True, default=None) | ||
children_by_color: Dict[Color, Child] = Relationship( | ||
sa_relationship_kwargs={"collection_class": attribute_keyed_dict("color")} | ||
) | ||
|
||
engine = create_engine("sqlite://") | ||
SQLModel.metadata.create_all(engine) | ||
with Session(engine) as session: | ||
parent = Parent() | ||
session.add(parent) | ||
session.commit() | ||
session.refresh(parent) | ||
session.add(Child(parent_id=parent.id, color=Color.Orange, value=1)) | ||
session.add(Child(parent_id=parent.id, color=Color.Blue, value=2)) | ||
session.commit() | ||
session.refresh(parent) | ||
assert parent.children_by_color[Color.Orange].parent_id == parent.id | ||
assert parent.children_by_color[Color.Orange].color == Color.Orange | ||
assert parent.children_by_color[Color.Orange].value == 1 | ||
assert parent.children_by_color[Color.Blue].parent_id == parent.id | ||
assert parent.children_by_color[Color.Blue].color == Color.Blue | ||
assert parent.children_by_color[Color.Blue].value == 2 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.