generated from MinBZK/python-project-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change sqlmodel to sqlalchemy (#133)
- Loading branch information
Showing
22 changed files
with
142 additions
and
163 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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,5 @@ | ||
from sqlalchemy.orm import DeclarativeBase | ||
|
||
|
||
class Base(DeclarativeBase): | ||
pass |
This file contains 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 |
---|---|---|
@@ -1,21 +1,16 @@ | ||
from pathlib import Path | ||
from typing import TypeVar | ||
|
||
from pydantic import field_validator | ||
from sqlmodel import Field, SQLModel # pyright: ignore [reportUnknownVariableType] | ||
from sqlalchemy import String | ||
from sqlalchemy.orm import Mapped, mapped_column | ||
|
||
T = TypeVar("T", bound="Project") | ||
from amt.models.base import Base | ||
|
||
T = TypeVar("T", bound="Project") | ||
|
||
class Project(SQLModel, table=True): | ||
id: int | None = Field(default=None, primary_key=True) | ||
name: str = Field(max_length=255, min_items=3) | ||
model_card: str | None = Field(description="Model card storage location", default=None) | ||
|
||
@field_validator("model_card") | ||
@classmethod | ||
def validate_model_card(cls: type[T], model_card: str) -> str: | ||
if not Path(model_card).is_file(): | ||
raise ValueError("Model card must be a file") | ||
class Project(Base): | ||
__tablename__ = "project" | ||
|
||
return model_card | ||
id: Mapped[int] = mapped_column(primary_key=True) | ||
name: Mapped[str] = mapped_column(String(255)) # TODO: (Christopher) how to set min_length? | ||
model_card: Mapped[str | None] = mapped_column(default=None) |
This file contains 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 |
---|---|---|
@@ -1,16 +1,20 @@ | ||
from sqlmodel import Field as SQLField # pyright: ignore [reportUnknownVariableType] | ||
from sqlmodel import SQLModel | ||
from sqlalchemy import ForeignKey | ||
from sqlalchemy.orm import Mapped, mapped_column | ||
|
||
from amt.models.base import Base | ||
|
||
class Task(SQLModel, table=True): | ||
id: int | None = SQLField(default=None, primary_key=True) | ||
title: str | ||
description: str | ||
sort_order: float | ||
status_id: int | None = SQLField(default=None) | ||
user_id: int | None = SQLField(default=None, foreign_key="user.id") | ||
|
||
class Task(Base): | ||
__tablename__ = "task" | ||
|
||
id: Mapped[int] = mapped_column(primary_key=True) | ||
title: Mapped[str] | ||
description: Mapped[str] | ||
sort_order: Mapped[float] | ||
status_id: Mapped[int | None] = mapped_column(default=None) | ||
user_id: Mapped[int | None] = mapped_column(ForeignKey("user.id")) | ||
# TODO: (Christopher) SQLModel does not allow to give the below restraint an name | ||
# which is needed for alembic. This results in changing the migration file | ||
# manually to give the restrain a name. | ||
project_id: int | None = SQLField(default=None, foreign_key="project.id") | ||
project_id: Mapped[int | None] = mapped_column(ForeignKey("project.id")) | ||
# todo(robbert) Tasks probably are grouped (and sub-grouped), so we probably need a reference to a group_id |
This file contains 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 |
---|---|---|
@@ -1,7 +1,11 @@ | ||
from sqlmodel import Field, SQLModel # pyright: ignore [reportUnknownVariableType] | ||
from sqlalchemy.orm import Mapped, mapped_column | ||
|
||
from amt.models.base import Base | ||
|
||
class User(SQLModel, table=True): | ||
id: int = Field(default=None, primary_key=True) | ||
name: str | ||
avatar: str | None | ||
|
||
class User(Base): | ||
__tablename__ = "user" | ||
|
||
id: Mapped[int] = mapped_column(primary_key=True) | ||
name: Mapped[str] | ||
avatar: Mapped[str | None] = mapped_column(default=None) |
This file contains 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 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 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 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
Oops, something went wrong.