【问题标题】:Alembic does not add Cascade information in autogenerate?Alembic 不会在自动生成中添加级联信息?
【发布时间】:2014-02-16 22:32:18
【问题描述】:

这是我的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

class BudgetCategory(db.Model):
    __tablename__ = 'budget_categories'
    # noinspection PyRedeclaration
    uuid = Column('uuid', GUID(), default=uuid.uuid4, primary_key=True,
                  unique=True)
    budget_id = Column(GUID(), ForeignKey('budgets.uuid'), nullable=False)
    budget = relationship('Budget', backref='budgetCategories',
                          cascade="all, delete-orphan", single_parent=True)
    category = Column('category', sa.types.String, nullable=True)
    parent_category = Column('parent_category', sa.types.String, nullable=True)
    amount = Column('amount', Numeric(10, 2), nullable=False)
    recurring = Column('recurring', sa.types.Boolean,
                       nullable=False)
    created_on = Column('created_on', sa.types.DateTime(timezone=True),
                        nullable=False)

当我生成 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 生成语法?我想我错过了一些东西,但不确定是什么?有人可以帮忙吗?

【问题讨论】:

  • 因为他们还没有实现这个功能。

标签: python orm sqlalchemy flask-sqlalchemy alembic


【解决方案1】:

ON DELETE CASCADE 语法需要在创建ForeignKeyForeignKeyConstraint 时显式配置:

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

【讨论】:

    猜你喜欢
    • 2022-07-01
    • 1970-01-01
    • 2013-08-09
    • 2015-08-06
    • 2019-03-10
    • 2012-06-26
    • 1970-01-01
    • 2020-11-05
    • 2015-12-14
    相关资源
    最近更新 更多