admin管理员组文章数量:1296312
I'm trying to get Alembic to autogenerate a first version of my database, which is at this point very simple. The command works, and generates a migration with all of my tables and columns, except all ForeignKey relations.
My code looks like this:
from typing import List
from sqlalchemy import String
from sqlalchemy.orm import DeclarativeBase, mapped_column, Mapped, relationship
class Base(DeclarativeBase):
pass
class DBUnitCategory(Base):
__tablename__ = 'unit_category'
id: Mapped[int] = mapped_column(primary_key=True)
name: Mapped[str] = mapped_column(String(20))
unit_id: Mapped[List["DBUnit"]] = relationship(back_populates="category_id")
class DBUnit(Base):
__tablename__ = 'unit'
id: Mapped[int] = mapped_column(primary_key=True)
name: Mapped[str] = mapped_column(String(12))
category_id: Mapped[DBUnitCategory] = relationship(back_populates="unit_id")
And the generated upgrade migration looks like this:
def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('unit',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('name', sa.String(length=12), nullable=False),
sa.PrimaryKeyConstraint('id')
)
op.create_table('unit_category',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('name', sa.String(length=20), nullable=False),
sa.PrimaryKeyConstraint('id')
)
# ### end Alembic commands ###
def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table('unit_category')
op.drop_table('unit')
I don't think I messed up my definitions, it looks like in the tutorial to me. There is no issue with this on the github, so I don't understand what's going on. Is it related to the fact I'm using an sqlite database?
I'm trying to get Alembic to autogenerate a first version of my database, which is at this point very simple. The command works, and generates a migration with all of my tables and columns, except all ForeignKey relations.
My code looks like this:
from typing import List
from sqlalchemy import String
from sqlalchemy.orm import DeclarativeBase, mapped_column, Mapped, relationship
class Base(DeclarativeBase):
pass
class DBUnitCategory(Base):
__tablename__ = 'unit_category'
id: Mapped[int] = mapped_column(primary_key=True)
name: Mapped[str] = mapped_column(String(20))
unit_id: Mapped[List["DBUnit"]] = relationship(back_populates="category_id")
class DBUnit(Base):
__tablename__ = 'unit'
id: Mapped[int] = mapped_column(primary_key=True)
name: Mapped[str] = mapped_column(String(12))
category_id: Mapped[DBUnitCategory] = relationship(back_populates="unit_id")
And the generated upgrade migration looks like this:
def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('unit',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('name', sa.String(length=12), nullable=False),
sa.PrimaryKeyConstraint('id')
)
op.create_table('unit_category',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('name', sa.String(length=20), nullable=False),
sa.PrimaryKeyConstraint('id')
)
# ### end Alembic commands ###
def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table('unit_category')
op.drop_table('unit')
I don't think I messed up my definitions, it looks like in the tutorial to me. There is no issue with this on the github, so I don't understand what's going on. Is it related to the fact I'm using an sqlite database?
Share Improve this question edited Feb 12 at 7:07 snakecharmerb 55.9k13 gold badges132 silver badges186 bronze badges asked Feb 11 at 21:39 S.ComeauS.Comeau 6310 bronze badges1 Answer
Reset to default 1Relationships create relationships between entities in the Python layer. You need to declare foreign key columns explicitly to create foreign keys in the database. You might want to review the documentation on relationship configuration.
Assuming that each category can have multiple units, you probably want something like this:
...
class DBUnitCategory(Base):
__tablename__ = 'unit_category'
id: Mapped[int] = mapped_column(primary_key=True)
name: Mapped[str] = mapped_column(String(20))
units: Mapped[List["DBUnit"]] = relationship(back_populates="category")
class DBUnit(Base):
__tablename__ = 'unit'
id: Mapped[int] = mapped_column(primary_key=True)
name: Mapped[str] = mapped_column(String(12))
category_id: Mapped[int] = mapped_column(ForeignKey('unit_category.id'))
category: Mapped[DBUnitCategory] = relationship(back_populates="units")
本文标签: pythonHow to make alembic autogenerate foreign key relationsStack Overflow
版权声明:本文标题:python - How to make alembic autogenerate foreign key relations? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741633307a2389493.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论