gpt4 book ai didi

python - 插入具有复合主键的模型时出现 IntegrityError

转载 作者:行者123 更新时间:2023-12-03 17:03:24 24 4
gpt4 key购买 nike

我正在尝试使用复合主键创建一个表。它有一个整数和两个字符串字段作为键。当我提交 session 时,它会引发 IntegrityError .我在做什么有什么问题?

class Targets(db.Model):
__tablename__ = 'Targets'
id = db.Column(db.Integer, primary_key=True)
Event_Name = db.Column(db.String, index=True, default='Single_Trigger', primary_key=True)
Trigger_id = db.Column(db.String, index=True, unique=True, primary_key=True)

>>> from app import db, models
>>> target = models.Targets(Trigger_id='20150421_221942_ST_1')
>>> db.session.add(target)
>>> db.session.commit()

IntegrityError: (IntegrityError) NOT NULL constraint failed: Targets.id u'INSERT INTO "Targets" ("Event_Name", "Name", "Trigger_id", "Tile", "Status", "RA", "Dec", "Filter", "Exp_time", "No_Exp") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)' ('Single_Trigger', '20150421_221942_ST_1', '20150421_221942_ST_1', 1, 0, 102.0, 231.25, 'V', 30, 5)

最佳答案

SQLAlchemy 不会在复合键上设置自动增量,您需要在 id 列上设置 autoincrement=True。

http://docs.sqlalchemy.org/en/latest/core/metadata.html#sqlalchemy.schema.Column.params.autoincrement

If the table has a composite primary key consisting of more than one integer column, set this flag to True only on the column that should be considered “autoincrement”.

关于python - 插入具有复合主键的模型时出现 IntegrityError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30232915/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com