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

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

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

问题

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

create table tbl(
    tbl_id INT generated always as identity PRIMARY KEY, 
    tbl_x  INT
) 

如何使用SQLAlchemy创建这个表?

英文:

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

create table tbl(
    tbl_id INT generated always as identity PRIMARY KEY, 
    tbl_x  INT
) 

How to create this table using sqlalchemy ?

答案1

得分: 1

Sure, here's the translated code snippet:

#...
from sqlalchemy import Identity
#...

tbl = Table('tbl', metadata_obj,
    Column('tbl_id', Integer, Identity(always=True)),
    Column('tbl_x', Integer),
)

查看:Identity

英文:
#...
from sqlalchemy import Identity
#...

tbl = Table('tbl', metadata_obj,
    Column('tbl_id', Integer, Identity(always=True))
    Column('tbl_x', Integer),
)

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:

确定