![](/img/trans.png)
[英]web2py: sqlite3.IntegrityError'>(foreign key constraint failed)
[英]FOREIGN KEY constraint failed in Web2py
我在table.py中有两个表-
db.define_table('spr_details',
Field('SPR_name', 'string', unique=True, notnull=True),
Field('Object_location',notnull=True),
Field('NSK_System',notnull=True,),
primarykey = ['SPR_name'],migrate=True)
db.define_table('my_master_table',
Field('Test_id',notnull=True),
Field('Test_suite',notnull=True),
Field('Test_category',notnull=True),
Field('Test_unit',notnull=True),
Field('Test_case',notnull=True),
Field ('Applicability', db.spr_details,'string'),migrate=True)
我在spr_details表中插入了一行。 现在,当我在my_master表中插入记录时,我从“适用性”下拉列中选择了先前插入的值。 但是提交后,我得到了
FOREIGN KEY约束失败错误。
以下是堆栈跟踪-
Traceback
Traceback (most recent call last):
File "C:\Users\pandeyar\Downloads\web2py_src\web2py\gluon\restricted.py", line 219, in restricted
exec(ccode, environment)
File "C:/Users/pandeyar/Downloads/web2py_src/web2py/applications/JDBC_E2E/controllers/default.py", line 183, in <module>
File "C:\Users\pandeyar\Downloads\web2py_src\web2py\gluon\globals.py", line 419, in <lambda>
self._caller = lambda f: f()
File "C:/Users/pandeyar/Downloads/web2py_src/web2py/applications/JDBC_E2E/controllers/default.py", line 99, in admin
user_signature=False,
File "C:\Users\pandeyar\Downloads\web2py_src\web2py\gluon\sqlhtml.py", line 3338, in smartgrid
user_signature=user_signature, **kwargs)
File "C:\Users\pandeyar\Downloads\web2py_src\web2py\gluon\sqlhtml.py", line 2534, in grid
onsuccess=oncreate)
File "C:\Users\pandeyar\Downloads\web2py_src\web2py\gluon\html.py", line 2300, in process
self.validate(**kwargs)
File "C:\Users\pandeyar\Downloads\web2py_src\web2py\gluon\html.py", line 2238, in validate
if self.accepts(**kwargs):
File "C:\Users\pandeyar\Downloads\web2py_src\web2py\gluon\sqlhtml.py", line 1965, in accepts
self.vars.id = self.table.insert(**fields)
File "C:\Users\pandeyar\Downloads\web2py_src\web2py\gluon\packages\dal\pydal\objects.py", line 753, in insert
ret = self._db._adapter.insert(self, row.op_values())
File "C:\Users\pandeyar\Downloads\web2py_src\web2py\gluon\packages\dal\pydal\adapters\base.py", line 486, in insert
raise e
File "C:\Users\pandeyar\Downloads\web2py_src\web2py\gluon\packages\dal\pydal\adapters\base.py", line 481, in insert
self.execute(query)
File "C:\Users\pandeyar\Downloads\web2py_src\web2py\gluon\packages\dal\pydal\adapters\__init__.py", line 67, in wrap
return f(*args, **kwargs)
File "C:\Users\pandeyar\Downloads\web2py_src\web2py\gluon\packages\dal\pydal\adapters\base.py", line 412, in execute
rv = self.cursor.execute(command, *args[1:], **kwargs)
sqlite3.IntegrityError: FOREIGN KEY constraint failed
任何帮助都将非常有帮助。
请参阅有关键控表 (即,其主键不是默认的自动递增整数ID字段的表)的spr_details
,因为spr_details
就是这样的表。
注意,您的参考字段定义存在两个问题:
Field('Applicability', db.spr_details, 'string')
首先,如文档中所述,在引用键表时, Field()
的type
参数的格式必须为'reference tablename.fieldname'
,而不是db.tablename
。 其次,您不必单独指定引用字段的类型(即,不应将'string'
作为第三个参数传递给Field()
,因为第三个参数是length
参数,而不是type
-推断type
)根据所引用字段的类型)。 因此,字段定义应为:
Field('Applicability', 'reference spr_details.SPR_name')
请注意,由于当前对“ Applicability
字段的定义不正确,该字段变成了一个整数字段(因为它希望在外部表中存储对整数ID字段的引用)。 如果您尝试插入整数,则会出现外键约束失败错误,并且如果您尝试插入字符串,则将出现关于在期望int
位置插入字符串的错误。 如果使用上面显示的正确字段定义并插入与db.spr_details.SPR_name
字段中存在的值匹配的字符串值,则不会出现任何错误。
还要注意,web2py假定将一起使用密钥表(即,被引用的表和引用表都将被密钥化)。 因此,举例来说,如果你使用SQLFORM
与my_master_table
,默认情况下它会假设,因为my_master_table
不键控,既不是spr_details
表,它引用。 因此,它将尝试将SPR_details
的值转换为整数ID(这将导致转换为0
,这将导致外键约束错误)。 要解决此问题,您应该手动处理数据库的插入和更新-例如:
form = SQLFORM(db.my_master_table).process(dbio=False)
if form.accepted:
db.my_master_table.insert(**form.vars)
在上面,设置dbio=False
可以防止SQLFORM
自己执行插入操作(这将导致错误)。 而是使用form.vars
的值显式完成插入。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.