Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 883
migration to sqlalchemy 2.0#563
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
60318b448ddc61b48423f9c219d937ed9790eee8e8050cf02fbff99c6d8f5274ce8d07ecfb3213738a7f814988b118ca3391707297f5ba1ce942e5eef9f00a643cea5b89adbbeff0803c5bdbcc1752f0bFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -3,6 +3,21 @@ | ||
| from sqlmodel import Field, Relationship, SQLModel, create_engine | ||
| class Hero(SQLModel, table=True): | ||
| id: Optional[int] = Field(default=None, primary_key=True) | ||
| name: str = Field(index=True) | ||
| secret_name: str | ||
| age: Optional[int] = Field(default=None, index=True) | ||
| team_id: Optional[int] = Field(default=None, foreign_key="team.id") | ||
| team: Optional["Team"] = Relationship(back_populates="heroes") | ||
| weapon_id: Optional[int] = Field(default=None, foreign_key="weapon.id") | ||
| weapon: Optional["Weapon"] = Relationship(back_populates="hero") | ||
| powers: List["Power"] = Relationship(back_populates="hero") | ||
farahats9 marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| class Weapon(SQLModel, table=True): | ||
| id: Optional[int] = Field(default=None, primary_key=True) | ||
| name: str = Field(index=True) | ||
| @@ -26,21 +41,6 @@ class Team(SQLModel, table=True): | ||
| heroes: List["Hero"] = Relationship(back_populates="team") | ||
| class Hero(SQLModel, table=True): | ||
| id: Optional[int] = Field(default=None, primary_key=True) | ||
| name: str = Field(index=True) | ||
| secret_name: str | ||
| age: Optional[int] = Field(default=None, index=True) | ||
| team_id: Optional[int] = Field(default=None, foreign_key="team.id") | ||
| team: Optional[Team] = Relationship(back_populates="heroes") | ||
| weapon_id: Optional[int] = Field(default=None, foreign_key="weapon.id") | ||
| weapon: Optional[Weapon] = Relationship(back_populates="hero") | ||
| powers: List[Power] = Relationship(back_populates="hero") | ||
| sqlite_file_name = "database.db" | ||
| sqlite_url = f"sqlite:///{sqlite_file_name}" | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -21,7 +21,6 @@ | ||
| from sqlalchemy.schema import PrimaryKeyConstraint as PrimaryKeyConstraint | ||
| from sqlalchemy.schema import Sequence as Sequence | ||
| from sqlalchemy.schema import Table as Table | ||
| from sqlalchemy.schema import ThreadLocalMetaData as ThreadLocalMetaData | ||
| from sqlalchemy.schema import UniqueConstraint as UniqueConstraint | ||
| from sqlalchemy.sql import alias as alias | ||
| from sqlalchemy.sql import all_ as all_ | ||
| @@ -71,7 +70,7 @@ | ||
| from sqlalchemy.sql import outerjoin as outerjoin | ||
| from sqlalchemy.sql import outparam as outparam | ||
| from sqlalchemy.sql import over as over | ||
| from sqlalchemy.sql import subquery as subquery | ||
farahats9 marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| from sqlalchemy.sql import Subquery as Subquery | ||
| from sqlalchemy.sql import table as table | ||
| from sqlalchemy.sql import tablesample as tablesample | ||
| from sqlalchemy.sql import text as text | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,29 +1,29 @@ | ||
| from typing import Generic, Iterator, List, Optional, TypeVar | ||
| from typing import Generic, Iterator, Optional, Sequence, Tuple, TypeVar | ||
| from sqlalchemy.engine.result import Result as _Result | ||
| from sqlalchemy.engine.result import ScalarResult as _ScalarResult | ||
| _T = TypeVar("_T") | ||
| class ScalarResult(_ScalarResult, Generic[_T]): | ||
| def all(self) -> List[_T]: | ||
| class ScalarResult(_ScalarResult[_T], Generic[_T]): | ||
| def all(self) -> Sequence[_T]: | ||
| return super().all() | ||
| def partitions(self, size: Optional[int] = None) -> Iterator[List[_T]]: | ||
| def partitions(self, size: Optional[int] = None) -> Iterator[Sequence[_T]]: | ||
| return super().partitions(size) | ||
| def fetchall(self) -> List[_T]: | ||
| def fetchall(self) -> Sequence[_T]: | ||
| return super().fetchall() | ||
| def fetchmany(self, size: Optional[int] = None) -> List[_T]: | ||
| def fetchmany(self, size: Optional[int] = None) -> Sequence[_T]: | ||
farahats9 marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| return super().fetchmany(size) | ||
| def __iter__(self) -> Iterator[_T]: | ||
| return super().__iter__() | ||
| def __next__(self) -> _T: | ||
| return super().__next__() # type: ignore | ||
| return super().__next__() | ||
| def first(self) -> Optional[_T]: | ||
| return super().first() | ||
| @@ -32,48 +32,8 @@ def one_or_none(self) -> Optional[_T]: | ||
| return super().one_or_none() | ||
| def one(self) -> _T: | ||
| return super().one() # type: ignore | ||
| return super().one() | ||
| class Result(_Result, Generic[_T]): | ||
| def scalars(self, index: int = 0) -> ScalarResult[_T]: | ||
| return super().scalars(index) # type: ignore | ||
| def __iter__(self) -> Iterator[_T]: # type: ignore | ||
| return super().__iter__() # type: ignore | ||
| def __next__(self) -> _T: # type: ignore | ||
| return super().__next__() # type: ignore | ||
| def partitions(self, size: Optional[int] = None) -> Iterator[List[_T]]: # type: ignore | ||
| return super().partitions(size) # type: ignore | ||
| def fetchall(self) -> List[_T]: # type: ignore | ||
| return super().fetchall() # type: ignore | ||
| def fetchone(self) -> Optional[_T]: # type: ignore | ||
| return super().fetchone() # type: ignore | ||
| def fetchmany(self, size: Optional[int] = None) -> List[_T]: # type: ignore | ||
| return super().fetchmany() # type: ignore | ||
| def all(self) -> List[_T]: # type: ignore | ||
| return super().all() # type: ignore | ||
| def first(self) -> Optional[_T]: # type: ignore | ||
| return super().first() # type: ignore | ||
| def one_or_none(self) -> Optional[_T]: # type: ignore | ||
| return super().one_or_none() # type: ignore | ||
| def scalar_one(self) -> _T: | ||
| return super().scalar_one() # type: ignore | ||
| def scalar_one_or_none(self) -> Optional[_T]: | ||
| return super().scalar_one_or_none() | ||
| def one(self) -> _T: # type: ignore | ||
| return super().one() # type: ignore | ||
| def scalar(self) -> Optional[_T]: | ||
| return super().scalar() | ||
| class Result(_Result[Tuple[_T]], Generic[_T]): | ||
| ... | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -11,6 +11,7 @@ | ||
| Callable, | ||
| ClassVar, | ||
| Dict, | ||
| ForwardRef, | ||
| List, | ||
| Mapping, | ||
| Optional, | ||
| @@ -29,7 +30,7 @@ | ||
| from pydantic.fields import FieldInfo as PydanticFieldInfo | ||
| from pydantic.fields import ModelField, Undefined, UndefinedType | ||
| from pydantic.main import ModelMetaclass, validate_model | ||
| from pydantic.typing import ForwardRef, NoArgAnyCallable, resolve_annotations | ||
| from pydantic.typing import NoArgAnyCallable, resolve_annotations | ||
| from pydantic.utils import ROOT_KEY, Representation | ||
| from sqlalchemy import Boolean, Column, Date, DateTime | ||
| from sqlalchemy import Enum as sa_Enum | ||
| @@ -478,6 +479,7 @@ class SQLModel(BaseModel, metaclass=SQLModelMetaclass, registry=default_registry | ||
| __sqlmodel_relationships__: ClassVar[Dict[str, RelationshipProperty]] # type: ignore | ||
| __name__: ClassVar[str] | ||
| metadata: ClassVar[MetaData] | ||
| __allow_unmapped__ = True # https://docs.sqlalchemy.org/en/20/changelog/migration_20.html#migration-20-step-six | ||
farahats9 marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| class Config: | ||
| orm_mode = True | ||
| @@ -521,7 +523,7 @@ def __setattr__(self, name: str, value: Any) -> None: | ||
| return | ||
| else: | ||
| # Set in SQLAlchemy, before Pydantic to trigger events and updates | ||
| if getattr(self.__config__, "table", False) and is_instrumented(self, name): | ||
| if getattr(self.__config__, "table", False) and is_instrumented(self, name): # type: ignore | ||
| set_attribute(self, name, value) | ||
| # Set in Pydantic model to trigger possible validation changes, only for | ||
| # non relationship values | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.