import sqlalchemy as sa
engine = sa.create_engine('postgresql:///test', echo=True)
tbl = sa.Table(
't40959264',
sa.MetaData(),
sa.Column('id', sa.Integer, sa.Identity(always=True), primary_key=True),
sa.Column('col', sa.Integer, sa.Identity(always=True, start=100, increment=10)),
)
tbl.drop(engine, checkfirst=True)
tbl.create(engine)
with engine.connect() as conn:
conn.execute(tbl.insert(), [{}] * 3)
conn.commit()
for row in conn.execute(sa.select(tbl)).mappings():
print(row)
生成以下DDL:
CREATE TABLE t40959264 (
id INTEGER GENERATED ALWAYS AS IDENTITY,
col INTEGER GENERATED ALWAYS AS IDENTITY (INCREMENT BY 10 START WITH 100),
PRIMARY KEY (id)
)
2条答案
按热度按时间jrcvhitl1#
像这样的东西应该工作。
smdnsysy2#
从PostgreSQL 10.0开始,您可以为此使用标识列(以及主键)。
例如以下代码:
生成以下DDL:
以及该输出: