-
-
Notifications
You must be signed in to change notification settings - Fork 846
✨ Support Annotated approach for Relationship attributes
#1192
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
bolu61
wants to merge
18
commits into
fastapi:main
Choose a base branch
from
bolu61:main
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.
Open
Changes from 16 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
435ad53
support PEP 593 for relationships
bolu61 c5d7df8
Fix formatting
bolu61 9f356db
Use Optional instead of `|`
bolu61 33604b4
Fix type annotation
bolu61 2534099
Use Any instead of type[Any]
bolu61 0ac4566
Merge branch 'main' into main
svlandeg ab9ed93
Merge branch 'main' into main
bolu61 1c9849d
Merge branch 'main' into main
bolu61 7bcbfaf
Merge branch 'fastapi:main' into main
bolu61 9d96e1d
Fix handling of Annotated types in relationship annotations
bolu61 bf364f8
📝 Add tutorial and tests for defining relationship attributes in SQLM…
bolu61 7c1919e
🎨 [pre-commit.ci] Auto format from pre-commit.com hooks
pre-commit-ci[bot] 40af690
Fix type hinting for relationship annotations in SQLModelMetaclass
bolu61 5e94c73
Merge branch 'main' into main_bolu61
svlandeg de8a2f4
🎨 Auto format
pre-commit-ci-lite[bot] 252b444
Simplify test
YuriiMotov e91fb22
Merge branch 'main' into bolu61/main
bolu61 9302e61
Address review feedback for Annotated relationships
bolu61 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
70 changes: 70 additions & 0 deletions
70
...c/tutorial/relationship_attributes/define_relationship_attributes/tutorial001_an_py310.py
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,70 @@ | ||
| from typing import Annotated | ||
|
|
||
| from sqlmodel import Field, Relationship, Session, SQLModel, create_engine | ||
|
|
||
|
|
||
| class Team(SQLModel, table=True): | ||
| id: Annotated[int | None, Field(primary_key=True)] = None | ||
| name: Annotated[str, Field(index=True)] | ||
| headquarters: str | ||
|
|
||
| heroes: Annotated[list["Hero"] | None, Relationship(back_populates="team")] = None | ||
|
|
||
|
|
||
| class Hero(SQLModel, table=True): | ||
| id: Annotated[int | None, Field(primary_key=True)] = None | ||
| name: Annotated[str, Field(index=True)] | ||
| secret_name: str | ||
| age: Annotated[int | None, Field(index=True)] = None | ||
|
|
||
| team_id: Annotated[int | None, Field(foreign_key="team.id")] = None | ||
| team: Annotated[Team | None, Relationship(back_populates="heroes")] = None | ||
|
|
||
|
|
||
| sqlite_file_name = "database.db" | ||
| sqlite_url = f"sqlite:///{sqlite_file_name}" | ||
|
|
||
| engine = create_engine(sqlite_url, echo=True) | ||
|
|
||
|
|
||
| def create_db_and_tables(): | ||
| SQLModel.metadata.create_all(engine) | ||
|
|
||
|
|
||
| def create_heroes(): | ||
| with Session(engine) as session: | ||
| team_preventers = Team(name="Preventers", headquarters="Sharp Tower") | ||
| team_z_force = Team(name="Z-Force", headquarters="Sister Margaret's Bar") | ||
|
|
||
| hero_deadpond = Hero( | ||
| name="Deadpond", secret_name="Dive Wilson", team=team_z_force | ||
| ) | ||
| hero_rusty_man = Hero( | ||
| name="Rusty-Man", secret_name="Tommy Sharp", age=48, team=team_preventers | ||
| ) | ||
| hero_spider_boy = Hero(name="Spider-Boy", secret_name="Pedro Parqueador") | ||
| session.add(hero_deadpond) | ||
| session.add(hero_rusty_man) | ||
| session.add(hero_spider_boy) | ||
| session.commit() | ||
|
|
||
| session.refresh(hero_deadpond) | ||
| session.refresh(hero_rusty_man) | ||
| session.refresh(hero_spider_boy) | ||
|
|
||
| print("Created hero:", hero_deadpond) | ||
| print("Created hero:", hero_rusty_man) | ||
| print("Created hero:", hero_spider_boy) | ||
|
|
||
| hero_spider_boy.team = team_preventers | ||
| session.add(hero_spider_boy) | ||
| session.commit() | ||
|
|
||
|
|
||
| def main(): | ||
| create_db_and_tables() | ||
| create_heroes() | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() |
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
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
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.