Skip to content

Commit

Permalink
migration to sqlalchemy 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
farahats9 committed Mar 7, 2023
1 parent 43a689d commit 60318b4
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 77 deletions.
30 changes: 15 additions & 15 deletions docs_src/tutorial/many_to_many/tutorial003.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,12 @@
from sqlmodel import Field, Relationship, Session, SQLModel, create_engine, select


class HeroTeamLink(SQLModel, table=True):
team_id: Optional[int] = Field(
default=None, foreign_key="team.id", primary_key=True
)
hero_id: Optional[int] = Field(
default=None, foreign_key="hero.id", primary_key=True
)
is_training: bool = False

team: "Team" = Relationship(back_populates="hero_links")
hero: "Hero" = Relationship(back_populates="team_links")


class Team(SQLModel, table=True):
id: Optional[int] = Field(default=None, primary_key=True)
name: str = Field(index=True)
headquarters: str

hero_links: List[HeroTeamLink] = Relationship(back_populates="team")
hero_links: List["HeroTeamLink"] = Relationship(back_populates="team")


class Hero(SQLModel, table=True):
Expand All @@ -30,7 +17,20 @@ class Hero(SQLModel, table=True):
secret_name: str
age: Optional[int] = Field(default=None, index=True)

team_links: List[HeroTeamLink] = Relationship(back_populates="hero")
team_links: List["HeroTeamLink"] = Relationship(back_populates="hero")


class HeroTeamLink(SQLModel, table=True):
team_id: Optional[int] = Field(
default=None, foreign_key="team.id", primary_key=True
)
hero_id: Optional[int] = Field(
default=None, foreign_key="hero.id", primary_key=True
)
is_training: bool = False

team: "Team" = Relationship(back_populates="hero_links")
hero: "Hero" = Relationship(back_populates="team_links")


sqlite_file_name = "database.db"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")


class Weapon(SQLModel, table=True):
id: Optional[int] = Field(default=None, primary_key=True)
name: str = Field(index=True)
Expand All @@ -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}"

Expand Down
6 changes: 2 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ classifiers = [
"Intended Audience :: System Administrators",
"License :: OSI Approved :: MIT License",
"Programming Language :: Python :: 3 :: Only",
"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
Expand All @@ -30,10 +29,9 @@ classifiers = [
]

[tool.poetry.dependencies]
python = "^3.6.1"
SQLAlchemy = ">=1.4.17,<=1.4.41"
python = "^3.7"
SQLAlchemy = ">=2.0.0,<=2.0.5.post1"
pydantic = "^1.8.2"
sqlalchemy2-stubs = {version = "*", allow-prereleases = true}

[tool.poetry.dev-dependencies]
pytest = "^7.0.1"
Expand Down
2 changes: 0 additions & 2 deletions sqlmodel/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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_
Expand Down Expand Up @@ -71,7 +70,6 @@
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
from sqlalchemy.sql import table as table
from sqlalchemy.sql import tablesample as tablesample
from sqlalchemy.sql import text as text
Expand Down
1 change: 1 addition & 0 deletions sqlmodel/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,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

class Config:
orm_mode = True
Expand Down
38 changes: 5 additions & 33 deletions sqlmodel/sql/expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,12 @@ class _Py36SelectOfScalar(_Select, Generic[_TSelect], metaclass=GenericSelectMet


@overload
def select(entity_0: _TScalar_0, **kw: Any) -> SelectOfScalar[_TScalar_0]: # type: ignore
def select(entity_0: _TScalar_0) -> SelectOfScalar[_TScalar_0]: # type: ignore
...


@overload
def select(entity_0: Type[_TModel_0], **kw: Any) -> SelectOfScalar[_TModel_0]: # type: ignore
def select(entity_0: Type[_TModel_0]) -> SelectOfScalar[_TModel_0]: # type: ignore
...


Expand All @@ -154,7 +154,6 @@ def select(entity_0: Type[_TModel_0], **kw: Any) -> SelectOfScalar[_TModel_0]:
def select( # type: ignore
entity_0: _TScalar_0,
entity_1: _TScalar_1,
**kw: Any,
) -> Select[Tuple[_TScalar_0, _TScalar_1]]:
...

Expand All @@ -163,7 +162,6 @@ def select( # type: ignore
def select( # type: ignore
entity_0: _TScalar_0,
entity_1: Type[_TModel_1],
**kw: Any,
) -> Select[Tuple[_TScalar_0, _TModel_1]]:
...

Expand All @@ -172,7 +170,6 @@ def select( # type: ignore
def select( # type: ignore
entity_0: Type[_TModel_0],
entity_1: _TScalar_1,
**kw: Any,
) -> Select[Tuple[_TModel_0, _TScalar_1]]:
...

Expand All @@ -181,7 +178,6 @@ def select( # type: ignore
def select( # type: ignore
entity_0: Type[_TModel_0],
entity_1: Type[_TModel_1],
**kw: Any,
) -> Select[Tuple[_TModel_0, _TModel_1]]:
...

Expand All @@ -191,7 +187,6 @@ def select( # type: ignore
entity_0: _TScalar_0,
entity_1: _TScalar_1,
entity_2: _TScalar_2,
**kw: Any,
) -> Select[Tuple[_TScalar_0, _TScalar_1, _TScalar_2]]:
...

Expand All @@ -201,7 +196,6 @@ def select( # type: ignore
entity_0: _TScalar_0,
entity_1: _TScalar_1,
entity_2: Type[_TModel_2],
**kw: Any,
) -> Select[Tuple[_TScalar_0, _TScalar_1, _TModel_2]]:
...

Expand All @@ -211,7 +205,6 @@ def select( # type: ignore
entity_0: _TScalar_0,
entity_1: Type[_TModel_1],
entity_2: _TScalar_2,
**kw: Any,
) -> Select[Tuple[_TScalar_0, _TModel_1, _TScalar_2]]:
...

Expand All @@ -221,7 +214,6 @@ def select( # type: ignore
entity_0: _TScalar_0,
entity_1: Type[_TModel_1],
entity_2: Type[_TModel_2],
**kw: Any,
) -> Select[Tuple[_TScalar_0, _TModel_1, _TModel_2]]:
...

Expand All @@ -231,7 +223,6 @@ def select( # type: ignore
entity_0: Type[_TModel_0],
entity_1: _TScalar_1,
entity_2: _TScalar_2,
**kw: Any,
) -> Select[Tuple[_TModel_0, _TScalar_1, _TScalar_2]]:
...

Expand All @@ -241,7 +232,6 @@ def select( # type: ignore
entity_0: Type[_TModel_0],
entity_1: _TScalar_1,
entity_2: Type[_TModel_2],
**kw: Any,
) -> Select[Tuple[_TModel_0, _TScalar_1, _TModel_2]]:
...

Expand All @@ -251,7 +241,6 @@ def select( # type: ignore
entity_0: Type[_TModel_0],
entity_1: Type[_TModel_1],
entity_2: _TScalar_2,
**kw: Any,
) -> Select[Tuple[_TModel_0, _TModel_1, _TScalar_2]]:
...

Expand All @@ -261,7 +250,6 @@ def select( # type: ignore
entity_0: Type[_TModel_0],
entity_1: Type[_TModel_1],
entity_2: Type[_TModel_2],
**kw: Any,
) -> Select[Tuple[_TModel_0, _TModel_1, _TModel_2]]:
...

Expand All @@ -272,7 +260,6 @@ def select( # type: ignore
entity_1: _TScalar_1,
entity_2: _TScalar_2,
entity_3: _TScalar_3,
**kw: Any,
) -> Select[Tuple[_TScalar_0, _TScalar_1, _TScalar_2, _TScalar_3]]:
...

Expand All @@ -283,7 +270,6 @@ def select( # type: ignore
entity_1: _TScalar_1,
entity_2: _TScalar_2,
entity_3: Type[_TModel_3],
**kw: Any,
) -> Select[Tuple[_TScalar_0, _TScalar_1, _TScalar_2, _TModel_3]]:
...

Expand All @@ -294,7 +280,6 @@ def select( # type: ignore
entity_1: _TScalar_1,
entity_2: Type[_TModel_2],
entity_3: _TScalar_3,
**kw: Any,
) -> Select[Tuple[_TScalar_0, _TScalar_1, _TModel_2, _TScalar_3]]:
...

Expand All @@ -305,7 +290,6 @@ def select( # type: ignore
entity_1: _TScalar_1,
entity_2: Type[_TModel_2],
entity_3: Type[_TModel_3],
**kw: Any,
) -> Select[Tuple[_TScalar_0, _TScalar_1, _TModel_2, _TModel_3]]:
...

Expand All @@ -316,7 +300,6 @@ def select( # type: ignore
entity_1: Type[_TModel_1],
entity_2: _TScalar_2,
entity_3: _TScalar_3,
**kw: Any,
) -> Select[Tuple[_TScalar_0, _TModel_1, _TScalar_2, _TScalar_3]]:
...

Expand All @@ -327,7 +310,6 @@ def select( # type: ignore
entity_1: Type[_TModel_1],
entity_2: _TScalar_2,
entity_3: Type[_TModel_3],
**kw: Any,
) -> Select[Tuple[_TScalar_0, _TModel_1, _TScalar_2, _TModel_3]]:
...

Expand All @@ -338,7 +320,6 @@ def select( # type: ignore
entity_1: Type[_TModel_1],
entity_2: Type[_TModel_2],
entity_3: _TScalar_3,
**kw: Any,
) -> Select[Tuple[_TScalar_0, _TModel_1, _TModel_2, _TScalar_3]]:
...

Expand All @@ -349,7 +330,6 @@ def select( # type: ignore
entity_1: Type[_TModel_1],
entity_2: Type[_TModel_2],
entity_3: Type[_TModel_3],
**kw: Any,
) -> Select[Tuple[_TScalar_0, _TModel_1, _TModel_2, _TModel_3]]:
...

Expand All @@ -360,7 +340,6 @@ def select( # type: ignore
entity_1: _TScalar_1,
entity_2: _TScalar_2,
entity_3: _TScalar_3,
**kw: Any,
) -> Select[Tuple[_TModel_0, _TScalar_1, _TScalar_2, _TScalar_3]]:
...

Expand All @@ -371,7 +350,6 @@ def select( # type: ignore
entity_1: _TScalar_1,
entity_2: _TScalar_2,
entity_3: Type[_TModel_3],
**kw: Any,
) -> Select[Tuple[_TModel_0, _TScalar_1, _TScalar_2, _TModel_3]]:
...

Expand All @@ -382,7 +360,6 @@ def select( # type: ignore
entity_1: _TScalar_1,
entity_2: Type[_TModel_2],
entity_3: _TScalar_3,
**kw: Any,
) -> Select[Tuple[_TModel_0, _TScalar_1, _TModel_2, _TScalar_3]]:
...

Expand All @@ -393,7 +370,6 @@ def select( # type: ignore
entity_1: _TScalar_1,
entity_2: Type[_TModel_2],
entity_3: Type[_TModel_3],
**kw: Any,
) -> Select[Tuple[_TModel_0, _TScalar_1, _TModel_2, _TModel_3]]:
...

Expand All @@ -404,7 +380,6 @@ def select( # type: ignore
entity_1: Type[_TModel_1],
entity_2: _TScalar_2,
entity_3: _TScalar_3,
**kw: Any,
) -> Select[Tuple[_TModel_0, _TModel_1, _TScalar_2, _TScalar_3]]:
...

Expand All @@ -415,7 +390,6 @@ def select( # type: ignore
entity_1: Type[_TModel_1],
entity_2: _TScalar_2,
entity_3: Type[_TModel_3],
**kw: Any,
) -> Select[Tuple[_TModel_0, _TModel_1, _TScalar_2, _TModel_3]]:
...

Expand All @@ -426,7 +400,6 @@ def select( # type: ignore
entity_1: Type[_TModel_1],
entity_2: Type[_TModel_2],
entity_3: _TScalar_3,
**kw: Any,
) -> Select[Tuple[_TModel_0, _TModel_1, _TModel_2, _TScalar_3]]:
...

Expand All @@ -437,18 +410,17 @@ def select( # type: ignore
entity_1: Type[_TModel_1],
entity_2: Type[_TModel_2],
entity_3: Type[_TModel_3],
**kw: Any,
) -> Select[Tuple[_TModel_0, _TModel_1, _TModel_2, _TModel_3]]:
...


# Generated overloads end


def select(*entities: Any, **kw: Any) -> Union[Select, SelectOfScalar]: # type: ignore
def select(*entities: Any) -> Union[Select, SelectOfScalar]: # type: ignore
if len(entities) == 1:
return SelectOfScalar._create(*entities, **kw) # type: ignore
return Select._create(*entities, **kw) # type: ignore
return SelectOfScalar(*entities) # type: ignore
return Select(*entities) # type: ignore


# TODO: add several @overload from Python types to SQLAlchemy equivalents
Expand Down
Loading

0 comments on commit 60318b4

Please sign in to comment.