如何使用 SQLAlchemy 插入复合主键?
How to insert composite primary key using SQLAlchemy?
当我尝试插入复合主键连接时 returns 0 行:
CompetitionRound = sa.Table('CompetitionRound', metadata,
sa.Column('CompetitionId', sa.Integer, sa.ForeignKey('Competitions.Id'), primary_key=True),
sa.Column('RoundId', sa.Integer, sa.ForeignKey('Rounds.Id'), primary_key=True))
...
competition_round_insert = await conn.execute(
CompetitionRound.insert()
.values(CompetitionId=competition_id,
RoundId=round_id))
competition_round_row = await competition_round_insert.fetchone()
似乎无法将复合主键作为值插入...但是在这种情况下如何插入键?
不幸的是,我没有在 SQLAlchemy 文档中找到一些例子......(
根据我对文档的理解,SQLAlchemy 仅隐式地 returns autoincrement
主键,而复合主键具体不是:
The autoincrement flag now defaults to "auto" which indicates autoincrement semantics by default for single-column integer primary keys only; for composite (multi-column) primary keys, autoincrement is never implicitly enabled; as always, autoincrement=True will allow for at most one of those columns to be an “autoincrement” column. autoincrement=True may also be set on a Column that has an explicit client-side or server-side default, subject to limitations of the backend database and dialect.
Note that primary key columns which specify a server_default clause, or otherwise do not qualify as “autoincrement” columns (see the notes at Column), and were generated using the database-side default, will appear in this list as None unless the backend supports “returning” and the insert statement executed with the “implicit returning” enabled.
意味着您可能想在插入内容中使用 an explicit returning clause。并不是说我真的明白这一点,你显然有相关的价值,因为你插入了它们。
当我尝试插入复合主键连接时 returns 0 行:
CompetitionRound = sa.Table('CompetitionRound', metadata,
sa.Column('CompetitionId', sa.Integer, sa.ForeignKey('Competitions.Id'), primary_key=True),
sa.Column('RoundId', sa.Integer, sa.ForeignKey('Rounds.Id'), primary_key=True))
...
competition_round_insert = await conn.execute(
CompetitionRound.insert()
.values(CompetitionId=competition_id,
RoundId=round_id))
competition_round_row = await competition_round_insert.fetchone()
似乎无法将复合主键作为值插入...但是在这种情况下如何插入键? 不幸的是,我没有在 SQLAlchemy 文档中找到一些例子......(
根据我对文档的理解,SQLAlchemy 仅隐式地 returns autoincrement
主键,而复合主键具体不是:
The autoincrement flag now defaults to "auto" which indicates autoincrement semantics by default for single-column integer primary keys only; for composite (multi-column) primary keys, autoincrement is never implicitly enabled; as always, autoincrement=True will allow for at most one of those columns to be an “autoincrement” column. autoincrement=True may also be set on a Column that has an explicit client-side or server-side default, subject to limitations of the backend database and dialect.
Note that primary key columns which specify a server_default clause, or otherwise do not qualify as “autoincrement” columns (see the notes at Column), and were generated using the database-side default, will appear in this list as None unless the backend supports “returning” and the insert statement executed with the “implicit returning” enabled.
意味着您可能想在插入内容中使用 an explicit returning clause。并不是说我真的明白这一点,你显然有相关的价值,因为你插入了它们。