如何使用SQLAlchemy创建一个类型为’generated always as identity’的主键。

huangapple go评论110阅读模式
英文:

How to create a primary key using sqlalchemy of type 'generated always as identity'

问题

我正在使用PostgreSQL,并希望为以下DDL创建SQLAlchemy模型:

  1. create table tbl(
  2. tbl_id INT generated always as identity PRIMARY KEY,
  3. tbl_x INT
  4. )

如何使用SQLAlchemy创建这个表?

英文:

I'm using postgres, and want to create the SQLA model for the following DDL:

  1. create table tbl(
  2. tbl_id INT generated always as identity PRIMARY KEY,
  3. tbl_x INT
  4. )

How to create this table using sqlalchemy ?

答案1

得分: 1

Sure, here's the translated code snippet:

  1. #...
  2. from sqlalchemy import Identity
  3. #...
  4. tbl = Table('tbl', metadata_obj,
  5. Column('tbl_id', Integer, Identity(always=True)),
  6. Column('tbl_x', Integer),
  7. )

查看:Identity

英文:
  1. #...
  2. from sqlalchemy import Identity
  3. #...
  4. tbl = Table('tbl', metadata_obj,
  5. Column('tbl_id', Integer, Identity(always=True))
  6. Column('tbl_x', Integer),
  7. )

See:Identity

huangapple
  • 本文由 发表于 2023年6月1日 06:09:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76377597.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定