【问题标题】:How to create an identity column not being a primary key with SQLAlchemy?如何使用 SQLAlchemy 创建一个不是主键的标识列?
【发布时间】:2016-12-04 13:44:40
【问题描述】:

如何在 SQLAlchemy 中创建一个标识列,自动递增而不是主键?

我正在使用 Postgres。

谢谢,

亚恩

【问题讨论】:

  • 哪个数据库?
  • 我正在使用 postgreSql
  • 你确定 postgresql 支持 不是 主要的自动增量列吗?例如,我知道在 mysql 中这是不可能
  • postgresql 支持序列,不需要链接到主键

标签: python sql sqlalchemy


【解决方案1】:

这样的事情应该可以工作。

table_sequence = Sequence('table_id_seq', start=1)

class MyTable(Base):
    __tablename__ = 'mytable'

    id = Column(Integer, table_sequence, server_default=table_sequence.next_value())

【讨论】:

  • 创建序列时出现此错误:TypeError: object() 不带参数
猜你喜欢
  • 1970-01-01
  • 2011-06-27
  • 1970-01-01
  • 1970-01-01
  • 2020-05-23
  • 2014-09-29
  • 1970-01-01
  • 1970-01-01
  • 2016-06-16
相关资源
最近更新 更多