Alembic不在autogenerate中添加层叠信息?

2024-05-12 21:29:24 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的Budget模式

class Budget(db.Model):
    __tablename__ = 'budgets'
    # noinspection PyRedeclaration
    uuid = Column('uuid', GUID(), default=uuid.uuid4, primary_key=True,
                  unique=True)
    user_id = Column(GUID(), ForeignKey('users.uuid'), nullable=False)
    user = relationship('User', backref='budgets')
    created_on = Column('created_on', sa.types.DateTime(timezone=True),
                        nullable=False)

BudgetCategories

^{pr2}$

当我生成alembic迁移脚本时

 alembic revision --autogenerate -m 'add_budgetCategories_Jan252014'

我得到了

def upgrade():
### commands auto generated by Alembic - please adjust! ###
    op.create_table('budgets',
    sa.Column('uuid', UUID(), nullable=False),
    sa.Column('user_id', UUID(), nullable=False),
    sa.Column('created_on', sa.DateTime(timezone=True), nullable=False),
    sa.ForeignKeyConstraint(['user_id'], ['users.uuid'], ),
    sa.PrimaryKeyConstraint('uuid'),
    sa.UniqueConstraint('uuid')
    )
    ### end Alembic commands ###

那我就知道了

 alembic revision --autogenerate -m 'add_budgetCategories_Jan252014'

我得到了

def upgrade():
### commands auto generated by Alembic - please adjust! ###
    op.create_table('budget_categories',
    sa.Column('uuid', sa.GUID(), nullable=False),
    sa.Column('budget_id', sa.GUID(), nullable=False),
    sa.Column('category', sa.String(), nullable=True),
    sa.Column('parent_category', sa.String(), nullable=True),
    sa.Column('amount', sa.Numeric(precision=10, scale=2), nullable=False),
    sa.Column('recurring', sa.Boolean(), nullable=False),
    sa.Column('created_on', sa.DateTime(timezone=True), nullable=False),
    sa.ForeignKeyConstraint(['budget_id'], ['budgets.uuid'], ),
    sa.PrimaryKeyConstraint('uuid'),
    sa.UniqueConstraint('uuid')
    )
    ### end Alembic commands ###

问题

为什么alembic没有为ON CASCADE DELETE生成语法?我想我错过了一些东西,但不确定是什么?有人能帮忙吗?在


Tags: idfalsetrueuuidonsacolumnalembic
1条回答
网友
1楼 · 发布于 2024-05-12 21:29:24

当您创建ForeignKeyForeignKeyConstraint时,需要显式地配置ON DELETE CASCADE语法:

ForeignKey("foo.id", ondelete="CASCADE")

这些设置与您在relationship()上使用“cascade='all,delete orphan'”这一事实没有直接联系,而且在任何情况下,您在“多对一”侧有级联设置,它甚至与外键中的任何内容都不相关。要使关系级联与ON DELETE cascade协同工作,它需要在一对多的一侧。在

相关文件:

http://docs.sqlalchemy.org/en/rel_0_9/core/constraints.html#on-update-and-on-delete

http://docs.sqlalchemy.org/en/rel_0_9/orm/session.html#unitofwork-cascades

http://docs.sqlalchemy.org/en/rel_0_9/orm/collections.html#using-passive-deletes

相关问题 更多 >