【问题标题】:How do I set an initial value for a primary key set to automatically increment using SQLAlchemy/Alembic?如何使用 SQLAlchemy/Alembic 设置主键集的初始值以自动递增?
【发布时间】:2012-10-13 15:46:59
【问题描述】:

在 MySQL 中,我们可以使用AUTO_INCREMENT = ? 来自动插入一个主键值。我们如何在 SQLAlchemy/Alembic 中做到这一点?非常感谢!

【问题讨论】:

    标签: mysql sqlalchemy alembic


    【解决方案1】:

    如果类型为“Integer, primary_key=True”,则在 CREATE TABLE 中发出 auto_increment。在 Alembic 中使用 alter_column,您需要将其指定为 alter_column。

    请参阅 alter_column() 的“自动增量”和“现有自动增量”:

    https://alembic.readthedocs.org/en/latest/ops.html#alembic.operations.Operations.alter_column

    查看 Column 的 autoincrement,注意这默认为 True 并且您通常不需要更改它,在:

    http://docs.sqlalchemy.org/en/rel_0_7/core/schema.html#sqlalchemy.schema.Column.__init__

    【讨论】:

    • 非常感谢,这很有帮助:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-16
    • 1970-01-01
    • 2021-01-19
    • 2014-12-02
    • 2014-01-17
    • 1970-01-01
    相关资源
    最近更新 更多