简体   繁体   English

Web2py中的FOREIGN KEY约束失败

[英]FOREIGN KEY constraint failed in Web2py

I've below two tables in tables.py - 我在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)

I inserted one row in spr_details table. 我在spr_details表中插入了一行。 Now when I am inserting record in my_master table, I am selecting previously inserted value from Applicability drop-down column. 现在,当我在my_master表中插入记录时,我从“适用性”下拉列中选择了先前插入的值。 But on submitting it, I am getting 但是提交后,我得到了

FOREIGN KEY constraint failed error. FOREIGN KEY约束失败错误。

below is the stack trace - 以下是堆栈跟踪-

Stack trace 堆栈跟踪

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

Any help would be extremely helpful. 任何帮助都将非常有帮助。

Please refer to the documentation on keyed tables (ie, tables whose primary key is not the default auto-incrementing integer ID field), as spr_details is such a table. 请参阅有关键控表 (即,其主键不是默认的自动递增整数ID字段的表)的spr_details ,因为spr_details就是这样的表。

Note, there are two problems with your reference field definition: 注意,您的参考字段定义存在两个问题:

    Field('Applicability', db.spr_details, 'string')

First, as noted in the documentation, when referencing a keyed table, the format for the type argument to Field() must be 'reference tablename.fieldname' , not db.tablename . 首先,如文档中所述,在引用键表时, Field()type参数的格式必须为'reference tablename.fieldname' ,而不是db.tablename Second, you do not separately specify the type of a reference field (ie, you should not pass 'string' as the third argument to Field() , as the third argument is the length argument, not the type -- the type is inferred based on the type of the referenced field). 其次,您不必单独指定引用字段的类型(即,不应将'string'作为第三个参数传递给Field() ,因为第三个参数是length参数,而不是type -推断type )根据所引用字段的类型)。 So, the field definition should be: 因此,字段定义应为:

    Field('Applicability', 'reference spr_details.SPR_name')

Note, with your current incorrect definition of the Applicability field, the field becomes an integer field (as it expects to store a reference to an integer ID field in the foreign table). 请注意,由于当前对“ Applicability字段的定义不正确,该字段变成了一个整数字段(因为它希望在外部表中存储对整数ID字段的引用)。 If you attempt to insert an integer, you will get a foreign key constraint failure error, and if you attempt to insert a string, you will get an error about inserting a string where an int is expected. 如果您尝试插入整数,则会出现外键约束失败错误,并且如果您尝试插入字符串,则将出现关于在期望int位置插入字符串的错误。 You should not get any errors if you use the correct field definition as shown above and insert string values that match values that exist in the db.spr_details.SPR_name field. 如果使用上面显示的正确字段定义并插入与db.spr_details.SPR_name字段中存在的值匹配的字符串值,则不会出现任何错误。

Also note that web2py assumes keyed tables will be used together (ie, both the referenced and the referencing table will be keyed). 还要注意,web2py假定将一起使用密钥表(即,被引用的表和引用表都将被密钥化)。 So, for example, if you use SQLFORM with my_master_table , by default it will assume that because my_master_table is not keyed, neither is the spr_details table that it references. 因此,举例来说,如果你使用SQLFORMmy_master_table ,默认情况下它会假设,因为my_master_table不键控,既不是spr_details表,它引用。 It will therefore attempt to convert the value of SPR_details to an integer ID (which results in a conversion to 0 , which leads to a foreign key constraint error). 因此,它将尝试将SPR_details的值转换为整数ID(这将导致转换为0 ,这将导致外键约束错误)。 To work around this, you should handle the database inserts and updates manually -- for example: 要解决此问题,您应该手动处理数据库的插入和更新-例如:

form = SQLFORM(db.my_master_table).process(dbio=False)
if form.accepted:
    db.my_master_table.insert(**form.vars)

Above, setting dbio=False prevents SQLFORM from doing the insert itself (which will result in an error). 在上面,设置dbio=False可以防止SQLFORM自己执行插入操作(这将导致错误)。 Instead, the insert is done explicitly using the values in form.vars . 而是使用form.vars的值显式完成插入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM