-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
back_up: trying to connect to db with ORM
- Loading branch information
1 parent
3e113f2
commit 55be9c7
Showing
16 changed files
with
701 additions
and
322 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
Empty file.
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,6 +1,6 @@ | ||
from fastapi import APIRouter | ||
|
||
from app.api.api_v1.endpoints import items | ||
from app.api.routes import items | ||
|
||
api_router = APIRouter() | ||
api_router.include_router(items.router, prefix="/items", tags=["items"]) |
File renamed without changes.
File renamed without changes.
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,35 @@ | ||
from sqlmodel import Session, select | ||
|
||
from app.api.deps import engine, get_super_client | ||
from app.core.config import settings | ||
from app.models import User | ||
|
||
# make sure all SQLModel models are imported (app.models) before initializing DB | ||
# otherwise, SQLModel might fail to initialize relationships properly | ||
# for more details: https://github.com/fastapi/full-stack-fastapi-template/issues/28 | ||
|
||
|
||
async def init_db(session: Session) -> None: | ||
# Tables should be created with Alembic migrations | ||
# But if you don't want to use migrations, create | ||
# the tables un-commenting the next lines | ||
from sqlmodel import SQLModel | ||
|
||
# This works because the models are already imported and registered from app.models | ||
SQLModel.metadata.create_all(engine) | ||
|
||
user = session.exec( | ||
select(User).where(User.email == settings.FIRST_SUPERUSER) | ||
).first() | ||
|
||
if not user: | ||
super_client = await get_super_client() | ||
response = await super_client.auth.sign_up( | ||
{ | ||
"email": settings.FIRST_SUPERUSER, | ||
"password": settings.FIRST_SUPERUSER_PASSWORD, | ||
} | ||
) | ||
assert response.user.email == settings.FIRST_SUPERUSER | ||
assert response.user.id is not None | ||
assert response.session.access_token is not 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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from sqlmodel import SQLModel | ||
|
||
from .item import Item | ||
from .user import User | ||
|
||
__all__ = ["User", "Item", "Message"] | ||
|
||
|
||
# Generic message | ||
class Message(SQLModel): | ||
message: str |
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,39 @@ | ||
import uuid | ||
|
||
from sqlmodel import Field, SQLModel | ||
|
||
|
||
# Shared properties | ||
class ItemBase(SQLModel): | ||
title: str = Field(min_length=1, max_length=255) | ||
description: str | None = Field(default=None, max_length=255) | ||
|
||
|
||
# Properties to receive on item creation | ||
class ItemCreate(ItemBase): | ||
pass | ||
|
||
|
||
# Properties to receive on item update | ||
class ItemUpdate(ItemBase): | ||
title: str | None = Field(default=None, min_length=1, max_length=255) # type: ignore | ||
|
||
|
||
# Database model, database table inferred from class name | ||
class Item(ItemBase, table=True): | ||
id: uuid.UUID = Field(default_factory=uuid.uuid4, primary_key=True) | ||
title: str = Field(max_length=255) | ||
owner_id: uuid.UUID = Field( | ||
foreign_key="user.id", nullable=False, ondelete="CASCADE" | ||
) | ||
|
||
|
||
# Properties to return via API, id is always required | ||
class ItemPublic(ItemBase): | ||
id: uuid.UUID | ||
owner_id: uuid.UUID | ||
|
||
|
||
class ItemsPublic(SQLModel): | ||
data: list[ItemPublic] | ||
count: int |
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,13 @@ | ||
import uuid | ||
|
||
from pydantic import EmailStr | ||
from sqlmodel import Field, Relationship, SQLModel | ||
|
||
from app.models import Item | ||
|
||
|
||
class User(SQLModel, table=True): | ||
# __table_args__ = {'extend_existing': True, 'autoload': True} | ||
id: uuid.UUID = Field(default_factory=uuid.uuid4, primary_key=True) | ||
email: EmailStr = Field(max_length=255) | ||
items: list["Item"] = Relationship(back_populates="owner", cascade_delete=True) |
Oops, something went wrong.