简体   繁体   English

如何修复Web2Py“ <class 'sqlite3.IntegrityError'> (外键约束失败)“?

[英]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.

相关问题 web2py:sqlite3.IntegrityError&#39;&gt;(外键约束失败) - web2py: sqlite3.IntegrityError'>(foreign key constraint failed) 表未监听其他表中外键设置的值,IntegrityError: (sqlite3.IntegrityError) NOT NULL 约束失败 - Table not listening to value set for foreign key in other table, IntegrityError: (sqlite3.IntegrityError) NOT NULL constraint failed Web2py中的FOREIGN KEY约束失败 - FOREIGN KEY constraint failed in Web2py sqlalchemy.exc.IntegrityError: (sqlite3.IntegrityError) NOT NULL 约束失败 - sqlalchemy.exc.IntegrityError: (sqlite3.IntegrityError) NOT NULL constraint failed 烧瓶-(sqlite3.IntegrityError)NOT NULL约束即使在迁移后也失败 - Flask - (sqlite3.IntegrityError) NOT NULL constraint failed even after migration IntegrityError:外键约束失败 - IntegrityError: FOREIGN KEY constraint failed Django 2.0:sqlite IntegrityError:外键约束失败 - Django 2.0: sqlite IntegrityError: FOREIGN KEY constraint failed 获取 sqlalchemy.exc.IntegrityError: (sqlite3.IntegrityError) NOT NULL 约束失败:user.image_file - Getting sqlalchemy.exc.IntegrityError: (sqlite3.IntegrityError) NOT NULL constraint failed: user.image_file sqlalchemy.exc.IntegrityError: (sqlite3.IntegrityError) NOT NULL 约束失败:user.id - sqlalchemy.exc.IntegrityError: (sqlite3.IntegrityError) NOT NULL constraint failed: user.id sqlalchemy.exc.IntegrityError: (sqlite3.IntegrityError) NOT NULL 约束失败:user.words - sqlalchemy.exc.IntegrityError: (sqlite3.IntegrityError) NOT NULL constraint failed: user.words
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM