我正在尝试创建一个迁移,以向现有表添加一个新的标识列。这个表最终应该成为那个表的新的主键。
class Action(db.Model):
id = db.Column(db.Integer(), db.Identity(), primary_key=True) # new primary key
uuid = db.Column(sqlalchemy_utils.UUIDType, index=True, nullable=False) # old primary key
当我使用flask db migrate
生成迁移时,我只得到一个新的整数列:
def upgrade():
"""Upgrade from previous version."""
op.add_column("actions", sa.Column("id", sa.Integer(), nullable=False))
对于基于id-column的新序列,我知道我需要显式地创建序列以将其用作server_default
(例如如下所述:https://sqlalchemy-alembic.narkive.com/U6p0nSGQ/adding-an-auto-increment-column-to-an-existing-table):
def upgrade():
"""Upgrade from previous version."""
op.execute(sa.schema.CreateSequence(sa.Sequence("actions_id_seq")))
op.add_column("actions", sa.Column("id", sa.Integer(), server_default=sa.text("nextval('actions_id_seq'::regclass)"), nullable=False))
使用sqlalchemy 1.4.42和alembic 1.8.1。
我找不到任何方法来添加一个身份列与GENERATED BY DEFAULT AS IDENTITY
。对如何做到这一点有什么建议吗?
似乎就像添加sa.Identity()
一样简单:
def upgrade():
"""Upgrade from previous version."""
op.add_column("actions", sa.Column("id", sa.Integer(), sa.Identity(), nullable=False))