weixin_39890652
weixin_39890652
2020-12-04 13:24

support PG INHERITS in autogenerate

Migrated issue, originally created by Quentin Leffray

Running Alembic autogenerate do not detect inherited columns from a parent table, and flag them as "to-be-dropped".

The model is defined as follow:


class Animal(Base):
    __tablename__ = 'animal'

    id = Column(Integer, primary_key=True)
    name = Column(String)
    type_ = Column(String)

    __mapper_args__ = {
        'polymorphic_identity': 'animal',
        'polymorphic_on': type_,
        'with_polymorphic': '*',
    }

class Unicorn(Animal):
    __tablename__ = 'unicorn'
    __table_args__ = {'postgresql_inherits': 'animal'}

    id = Column(Integer, ForeignKey('animal.id'), primary_key=True)
    color = Column(String)

    __mapper_args__ = {
        'polymorphic_identity': 'unicorn',
    }

And the migration produced by alembic --autogenerate:


def upgrade():
    ### commands auto generated by Alembic - please adjust! ###
    op.drop_column('unicorn', 'name')
    op.drop_column('unicorn', 'type_')

该提问来源于开源项目:sqlalchemy/alembic

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

4条回答

为你推荐

换一换