[英]How to fix Web2Py “<class 'sqlite3.IntegrityError'>(foreign key constraint failed)”?
I am attempting to add a new record into a database table that references another table and I am receiving this error from Web2Py: "(foreign key constraint failed)". 我试图将新记录添加到引用另一个表的数据库表中,我从Web2Py收到此错误:“(外键约束失败)”。
I am following the Web2Py books example as seen below: 我正在关注Web2Py书籍示例,如下所示:
Chapter 7: Forms and validators
第7章:表单和验证器
Links to referencing records
引用记录的链接
Now consider the case of two tables linked by a reference field.
现在考虑由引用字段链接的两个表的情况。 For example:
例如:
db.define_table('person', Field('name', requires=IS_NOT_EMPTY())) db.define_table('dog', Field('owner', 'reference person'), Field('name', requires=IS_NOT_EMPTY())) db.dog.owner.requires = IS_IN_DB(db, 'person.id', '%(name)s')
A person has dogs, and each dog belongs to an owner, which is a person.
一个人有狗,每只狗属于一个人,这是一个人。 The dog owner is required to reference a valid db.person.id by '%(name)s'.
狗主人需要通过'%(name)s'引用有效的db.person.id。
This is where I am confused because I believe I am following the example unless I am really oblivious to my errors. 这是我感到困惑的地方,因为我相信我会关注这个例子,除非我真的忘记了我的错误。
The model I am using is below. 我正在使用的模型如下。
I am attempting to create an album the requires an artist to exist, which the artist record does, from a form. 我试图从一个表单创建一个需要艺术家存在的专辑,这是艺术家唱片所做的。
db.define_table('artist',
Field('name', requires=(IS_SLUG(),
IS_NOT_IN_DB(db, 'artist.name'))),
auth.signature)
# IS_SLUG removes special chars, IS_NOT_IN_DB makes it unique
db.define_table('album',
Field('name', requires=(IS_SLUG(),
IS_NOT_IN_DB(db, 'album.name'))),
Field('artist_id', 'reference artist'), ### HERE ###
Field('album_art', 'upload'),
Field('chapter', 'integer', requires=IS_NOT_EMPTY()),
Field('act', 'integer', default=0),
Field('release_date', 'date', requires=IS_NOT_EMPTY()),
Field('num_of_tracks', 'integer', default=0),
auth.signature)
db.album.artist_id.requires = IS_IN_DB(db, 'artist.id', '%(name)s')
I expected the form to insert the album, the same way that the artist form did. 我希望表单插入专辑,就像艺术家表单一样。
I know the reference is causing the issue and I would really appreciate if anyone could help me see my errors. 我知道引用引起了这个问题,如果有人能帮我看看我的错误,我真的很感激。
The latest Firefox Developer Edition is having issues with Web2Py. 最新的Firefox Developer Edition存在Web2Py问题。
I switched to Chrome and everything works. 我切换到Chrome,一切正常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.