mirror of
https://github.com/grillazz/fastapi-sqlalchemy-asyncpg.git
synced 2025-08-26 16:40:40 +03:00
Merge pull request #124 from grillazz/2-add-model-relations-in-async-environment
m-2-m relation sample
This commit is contained in:
commit
c0ece73817
@ -1,40 +0,0 @@
|
|||||||
"""user auth
|
|
||||||
|
|
||||||
Revision ID: 2dcc708f88f8
|
|
||||||
Revises: 0d1ee3949d21
|
|
||||||
Create Date: 2023-07-22 12:19:28.780926
|
|
||||||
|
|
||||||
"""
|
|
||||||
from alembic import op
|
|
||||||
import sqlalchemy as sa
|
|
||||||
|
|
||||||
|
|
||||||
# revision identifiers, used by Alembic.
|
|
||||||
revision = '2dcc708f88f8'
|
|
||||||
down_revision = '0d1ee3949d21'
|
|
||||||
branch_labels = None
|
|
||||||
depends_on = None
|
|
||||||
|
|
||||||
|
|
||||||
def upgrade():
|
|
||||||
# ### commands auto generated by Alembic - please adjust! ###
|
|
||||||
op.create_table('user',
|
|
||||||
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),
|
|
||||||
sa.Column('password', sa.LargeBinary(), nullable=False),
|
|
||||||
sa.PrimaryKeyConstraint('id'),
|
|
||||||
sa.UniqueConstraint('id')
|
|
||||||
)
|
|
||||||
op.create_unique_constraint(None, 'nonsense', ['name'], schema='happy_hog')
|
|
||||||
op.create_unique_constraint(None, 'stuff', ['name'], schema='happy_hog')
|
|
||||||
# ### end Alembic commands ###
|
|
||||||
|
|
||||||
|
|
||||||
def downgrade():
|
|
||||||
# ### commands auto generated by Alembic - please adjust! ###
|
|
||||||
op.drop_constraint(None, 'stuff', schema='happy_hog', type_='unique')
|
|
||||||
op.drop_constraint(None, 'nonsense', schema='happy_hog', type_='unique')
|
|
||||||
op.drop_table('user')
|
|
||||||
# ### end Alembic commands ###
|
|
@ -1,16 +1,16 @@
|
|||||||
"""init migrations
|
"""
|
||||||
|
|
||||||
Revision ID: 0d1ee3949d21
|
Revision ID: 68cd4c3a0af0
|
||||||
Revises:
|
Revises:
|
||||||
Create Date: 2023-03-11 20:08:07.362613
|
Create Date: 2024-01-02 20:12:46.214651
|
||||||
|
|
||||||
"""
|
"""
|
||||||
from alembic import op
|
from alembic import op
|
||||||
import sqlalchemy as sa
|
import sqlalchemy as sa
|
||||||
from sqlalchemy.dialects import postgresql
|
|
||||||
|
|
||||||
# revision identifiers, used by Alembic.
|
# revision identifiers, used by Alembic.
|
||||||
revision = '0d1ee3949d21'
|
revision = '68cd4c3a0af0'
|
||||||
down_revision = None
|
down_revision = None
|
||||||
branch_labels = None
|
branch_labels = None
|
||||||
depends_on = None
|
depends_on = None
|
||||||
@ -19,18 +19,18 @@ depends_on = None
|
|||||||
def upgrade():
|
def upgrade():
|
||||||
# ### commands auto generated by Alembic - please adjust! ###
|
# ### commands auto generated by Alembic - please adjust! ###
|
||||||
op.create_table('nonsense',
|
op.create_table('nonsense',
|
||||||
sa.Column('id', sa.UUID(), autoincrement=True, nullable=True),
|
sa.Column('id', sa.UUID(), autoincrement=True, nullable=False),
|
||||||
sa.Column('name', sa.String(), nullable=False),
|
sa.Column('name', sa.String(), nullable=False),
|
||||||
sa.Column('description', sa.String(), nullable=False),
|
sa.Column('description', sa.String(), nullable=True),
|
||||||
sa.PrimaryKeyConstraint('name'),
|
sa.PrimaryKeyConstraint('name'),
|
||||||
sa.UniqueConstraint('id'),
|
sa.UniqueConstraint('id'),
|
||||||
sa.UniqueConstraint('name'),
|
sa.UniqueConstraint('name'),
|
||||||
schema='happy_hog'
|
schema='happy_hog'
|
||||||
)
|
)
|
||||||
op.create_table('stuff',
|
op.create_table('stuff',
|
||||||
sa.Column('id', sa.UUID(), autoincrement=True, nullable=True),
|
sa.Column('id', sa.UUID(), autoincrement=True, nullable=False),
|
||||||
sa.Column('name', sa.String(), nullable=False),
|
sa.Column('name', sa.String(), nullable=False),
|
||||||
sa.Column('description', sa.String(), nullable=False),
|
sa.Column('description', sa.String(), nullable=True),
|
||||||
sa.PrimaryKeyConstraint('name'),
|
sa.PrimaryKeyConstraint('name'),
|
||||||
sa.UniqueConstraint('id'),
|
sa.UniqueConstraint('id'),
|
||||||
sa.UniqueConstraint('name'),
|
sa.UniqueConstraint('name'),
|
||||||
@ -67,6 +67,25 @@ def upgrade():
|
|||||||
sa.PrimaryKeyConstraint('id', name='work_pkey'),
|
sa.PrimaryKeyConstraint('id', name='work_pkey'),
|
||||||
schema='shakespeare'
|
schema='shakespeare'
|
||||||
)
|
)
|
||||||
|
op.create_table('user',
|
||||||
|
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),
|
||||||
|
sa.Column('_password', sa.LargeBinary(), nullable=False),
|
||||||
|
sa.PrimaryKeyConstraint('id'),
|
||||||
|
sa.UniqueConstraint('email')
|
||||||
|
)
|
||||||
|
op.create_table('stuff_full_of_nonsense',
|
||||||
|
sa.Column('id', sa.UUID(), nullable=False),
|
||||||
|
sa.Column('stuff_id', sa.UUID(), nullable=False),
|
||||||
|
sa.Column('nonsense_id', sa.UUID(), nullable=False),
|
||||||
|
sa.Column('but_why', sa.String(), nullable=True),
|
||||||
|
sa.ForeignKeyConstraint(['nonsense_id'], ['happy_hog.nonsense.id'], ),
|
||||||
|
sa.ForeignKeyConstraint(['stuff_id'], ['happy_hog.stuff.id'], ),
|
||||||
|
sa.PrimaryKeyConstraint('id'),
|
||||||
|
schema='happy_hog'
|
||||||
|
)
|
||||||
op.create_table('chapter',
|
op.create_table('chapter',
|
||||||
sa.Column('id', sa.Integer(), nullable=False),
|
sa.Column('id', sa.Integer(), nullable=False),
|
||||||
sa.Column('work_id', sa.String(length=32), nullable=False),
|
sa.Column('work_id', sa.String(length=32), nullable=False),
|
||||||
@ -113,6 +132,8 @@ def downgrade():
|
|||||||
op.drop_table('paragraph', schema='shakespeare')
|
op.drop_table('paragraph', schema='shakespeare')
|
||||||
op.drop_table('character_work', schema='shakespeare')
|
op.drop_table('character_work', schema='shakespeare')
|
||||||
op.drop_table('chapter', schema='shakespeare')
|
op.drop_table('chapter', schema='shakespeare')
|
||||||
|
op.drop_table('stuff_full_of_nonsense', schema='happy_hog')
|
||||||
|
op.drop_table('user')
|
||||||
op.drop_table('work', schema='shakespeare')
|
op.drop_table('work', schema='shakespeare')
|
||||||
op.drop_table('wordform', schema='shakespeare')
|
op.drop_table('wordform', schema='shakespeare')
|
||||||
op.drop_table('character', schema='shakespeare')
|
op.drop_table('character', schema='shakespeare')
|
@ -1,12 +1,13 @@
|
|||||||
import uuid
|
import uuid
|
||||||
|
|
||||||
from fastapi import HTTPException, status
|
from fastapi import HTTPException, status
|
||||||
from sqlalchemy import String, select
|
from sqlalchemy import String, select, ForeignKey
|
||||||
from sqlalchemy.dialects.postgresql import UUID
|
from sqlalchemy.dialects.postgresql import UUID
|
||||||
from sqlalchemy.ext.asyncio import AsyncSession
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
from sqlalchemy.orm import mapped_column, Mapped
|
from sqlalchemy.orm import mapped_column, Mapped, relationship, joinedload
|
||||||
|
|
||||||
from app.models.base import Base
|
from app.models.base import Base
|
||||||
|
from app.models.nonsense import Nonsense
|
||||||
|
|
||||||
|
|
||||||
class Stuff(Base):
|
class Stuff(Base):
|
||||||
@ -16,6 +17,8 @@ class Stuff(Base):
|
|||||||
name: Mapped[str] = mapped_column(String, primary_key=True, unique=True)
|
name: Mapped[str] = mapped_column(String, primary_key=True, unique=True)
|
||||||
description: Mapped[str | None]
|
description: Mapped[str | None]
|
||||||
|
|
||||||
|
nonsense: Mapped["Nonsense"] = relationship("Nonsense", secondary="happy_hog.stuff_full_of_nonsense")
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
async def find(cls, db_session: AsyncSession, name: str):
|
async def find(cls, db_session: AsyncSession, name: str):
|
||||||
"""
|
"""
|
||||||
@ -24,7 +27,11 @@ class Stuff(Base):
|
|||||||
:param name:
|
:param name:
|
||||||
:return:
|
:return:
|
||||||
"""
|
"""
|
||||||
stmt = select(cls).where(cls.name == name)
|
stmt = (
|
||||||
|
select(cls)
|
||||||
|
.options(joinedload(cls.nonsense))
|
||||||
|
.where(cls.name == name)
|
||||||
|
)
|
||||||
result = await db_session.execute(stmt)
|
result = await db_session.execute(stmt)
|
||||||
instance = result.scalars().first()
|
instance = result.scalars().first()
|
||||||
if instance is None:
|
if instance is None:
|
||||||
@ -34,3 +41,12 @@ class Stuff(Base):
|
|||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
return instance
|
return instance
|
||||||
|
|
||||||
|
|
||||||
|
class StuffFullOfNonsense(Base):
|
||||||
|
__tablename__ = "stuff_full_of_nonsense"
|
||||||
|
__table_args__ = ({"schema": "happy_hog"},)
|
||||||
|
id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), default=uuid.uuid4, primary_key=True)
|
||||||
|
stuff_id: Mapped[Stuff] = mapped_column(UUID, ForeignKey("happy_hog.stuff.id"))
|
||||||
|
nonsense_id: Mapped["Nonsense"] = mapped_column(UUID, ForeignKey("happy_hog.nonsense.id"))
|
||||||
|
but_why: Mapped[str | None]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user