mirror of
https://github.com/grillazz/fastapi-sqlalchemy-asyncpg.git
synced 2026-01-17 11:40:39 +03:00
apply sqlalchemy 2.0 declarative base and mapped columns
This commit is contained in:
@@ -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):
|
||||
|
||||
Reference in New Issue
Block a user