0

我在 tables.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

任何帮助都会非常有帮助。

4

1 回答 1

0

请参阅有关键控表的文档(即,主键不是默认的自动递增整数 ID 字段的表),就像spr_details这样的表。

请注意,您的参考字段定义存在两个问题:

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

首先,如文档中所述,在引用键控表时,type参数 to的格式Field()必须是'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 假设键控表将一起使用(即,被引用表和引用表都将被键控)。因此,例如,如果您使用SQLFORMwith 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.

于 2018-05-28T15:13:13.557 回答