mirror of
https://github.com/grillazz/fastapi-sqlalchemy-asyncpg.git
synced 2025-08-26 16:40:40 +03:00
apply sqlalchemy 2.0 declarative base and mapped columns
This commit is contained in:
parent
f5de28ec5f
commit
d0372e4420
@ -19,7 +19,7 @@ depends_on = None
|
||||
def upgrade():
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.create_table('user',
|
||||
sa.Column('uuid', sa.UUID(), nullable=False),
|
||||
sa.Column('id', sa.UUID(), nullable=False),
|
||||
sa.Column('email', sa.String(), nullable=False),
|
||||
sa.Column('first_name', sa.String(), nullable=False),
|
||||
sa.Column('last_name', sa.String(), nullable=False),
|
||||
|
@ -10,7 +10,7 @@ router = APIRouter(prefix="/v1/nonsense")
|
||||
|
||||
@router.post("/", status_code=status.HTTP_201_CREATED, response_model=NonsenseResponse)
|
||||
async def create_nonsense(payload: NonsenseSchema, db_session: AsyncSession = Depends(get_db)):
|
||||
nonsense = Nonsense(**payload.dict())
|
||||
nonsense = Nonsense(**payload.model_dump())
|
||||
await nonsense.save(db_session)
|
||||
return nonsense
|
||||
|
||||
|
@ -15,7 +15,7 @@ router = APIRouter(prefix="/v1/stuff")
|
||||
@router.post("/add_many", status_code=status.HTTP_201_CREATED)
|
||||
async def create_multi_stuff(payload: list[StuffSchema], db_session: AsyncSession = Depends(get_db)):
|
||||
try:
|
||||
stuff_instances = [Stuff(name=stuf.name, description=stuf.description) for stuf in payload]
|
||||
stuff_instances = [Stuff(**stuff.model_dump()) for stuff in payload]
|
||||
db_session.add_all(stuff_instances)
|
||||
await db_session.commit()
|
||||
except SQLAlchemyError as ex:
|
||||
@ -28,7 +28,7 @@ async def create_multi_stuff(payload: list[StuffSchema], db_session: AsyncSessio
|
||||
|
||||
@router.post("", status_code=status.HTTP_201_CREATED, response_model=StuffResponse)
|
||||
async def create_stuff(payload: StuffSchema, db_session: AsyncSession = Depends(get_db)):
|
||||
stuff = Stuff(name=payload.name, description=payload.description)
|
||||
stuff = Stuff(**payload.model_dump())
|
||||
await stuff.save(db_session)
|
||||
return stuff
|
||||
|
||||
|
@ -1,9 +1,10 @@
|
||||
import uuid
|
||||
|
||||
from fastapi import HTTPException, status
|
||||
from sqlalchemy import Column, String, select
|
||||
from sqlalchemy import String, select
|
||||
from sqlalchemy.dialects.postgresql import UUID
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from sqlalchemy.orm import mapped_column, Mapped
|
||||
|
||||
from app.models.base import Base
|
||||
|
||||
@ -11,9 +12,9 @@ from app.models.base import Base
|
||||
class Nonsense(Base):
|
||||
__tablename__ = "nonsense"
|
||||
__table_args__ = ({"schema": "happy_hog"},)
|
||||
id = Column(UUID(as_uuid=True), unique=True, default=uuid.uuid4, autoincrement=True)
|
||||
name = Column(String, nullable=False, primary_key=True, unique=True)
|
||||
description = Column(String, nullable=False)
|
||||
id: Mapped[uuid:UUID] = mapped_column(UUID(as_uuid=True), unique=True, default=uuid.uuid4, autoincrement=True)
|
||||
name: Mapped[str] = mapped_column(String, nullable=False, primary_key=True, unique=True)
|
||||
description: Mapped[str] = mapped_column(String, nullable=False)
|
||||
|
||||
@classmethod
|
||||
async def find(cls, db_session: AsyncSession, name: str):
|
||||
@ -33,3 +34,4 @@ class Nonsense(Base):
|
||||
)
|
||||
else:
|
||||
return instance
|
||||
|
||||
|
@ -11,7 +11,7 @@ from sqlalchemy import (
|
||||
select,
|
||||
)
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from sqlalchemy.orm import relationship
|
||||
from sqlalchemy.orm import relationship, mapped_column, Mapped
|
||||
|
||||
from app.models.base import Base
|
||||
|
||||
|
@ -1,9 +1,10 @@
|
||||
import uuid
|
||||
|
||||
from fastapi import HTTPException, status
|
||||
from sqlalchemy import Column, String, select
|
||||
from sqlalchemy import String, select
|
||||
from sqlalchemy.dialects.postgresql import UUID
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from sqlalchemy.orm import mapped_column, Mapped
|
||||
|
||||
from app.models.base import Base
|
||||
|
||||
@ -11,9 +12,9 @@ from app.models.base import Base
|
||||
class Stuff(Base):
|
||||
__tablename__ = "stuff"
|
||||
__table_args__ = ({"schema": "happy_hog"},)
|
||||
id = Column(UUID(as_uuid=True), unique=True, default=uuid.uuid4, autoincrement=True)
|
||||
name = Column(String, nullable=False, primary_key=True, unique=True)
|
||||
description = Column(String, nullable=False)
|
||||
id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), unique=True, default=uuid.uuid4, autoincrement=True)
|
||||
name: Mapped[str] = mapped_column(String, nullable=False, primary_key=True, unique=True)
|
||||
description: Mapped[str] = mapped_column(String, nullable=False)
|
||||
|
||||
@classmethod
|
||||
async def find(cls, db_session: AsyncSession, name: str):
|
||||
|
@ -6,6 +6,7 @@ from passlib.context import CryptContext
|
||||
from sqlalchemy import Column, String, LargeBinary, select
|
||||
from sqlalchemy.dialects.postgresql import UUID
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from sqlalchemy.orm import mapped_column, Mapped
|
||||
|
||||
from app import config
|
||||
from app.models.base import Base
|
||||
@ -15,23 +16,12 @@ global_settings = config.get_settings()
|
||||
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
|
||||
|
||||
|
||||
class User(Base): # type: ignore
|
||||
uuid = Column(
|
||||
UUID(as_uuid=True),
|
||||
unique=True,
|
||||
default=uuid.uuid4,
|
||||
primary_key=True,
|
||||
)
|
||||
email = Column(String, nullable=False)
|
||||
first_name = Column(String, nullable=False)
|
||||
last_name = Column(String, nullable=False)
|
||||
_password = Column("password", LargeBinary, nullable=False)
|
||||
|
||||
def __init__(self, email: str, first_name: str, last_name: str, password: str = None):
|
||||
self.email = email
|
||||
self.first_name = first_name
|
||||
self.last_name = last_name
|
||||
self.password = password
|
||||
class User(Base):
|
||||
id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), default=uuid.uuid4, primary_key=True)
|
||||
email: Mapped[str] = mapped_column(String, nullable=False, unique=True)
|
||||
first_name: Mapped[str] = mapped_column(String, nullable=False)
|
||||
last_name: Mapped[str] = mapped_column(String, nullable=False)
|
||||
_password: Mapped[bytes] = mapped_column(LargeBinary, nullable=False)
|
||||
|
||||
@property
|
||||
def password(self):
|
||||
|
@ -15,7 +15,7 @@ class UserSchema(BaseModel):
|
||||
|
||||
|
||||
class UserResponse(BaseModel):
|
||||
uuid: UUID = Field(title="User’s id", description="User’s id")
|
||||
id: UUID = Field(title="User’s id", description="User’s id")
|
||||
email: EmailStr = Field(title="User’s email", description="User’s email")
|
||||
first_name: str = Field(title="User’s first name", description="User’s first name")
|
||||
last_name: str = Field(title="User’s last name", description="User’s last name")
|
||||
|
Loading…
x
Reference in New Issue
Block a user