web2py:sqlite3.IntegrityError'>(外键约束失败)
web2py: sqlite3.IntegrityError'>(foreign key constraint failed)
我知道这是第 n 次在论坛上提出这个问题了,但是我需要你的帮助,因为我无法弄清楚我做错了什么。
型号
db = DAL('sqlite://storage.sqlite',pool_size=1,check_reserved=['all'])
auth = Auth(db)
service = Service()
plugins = PluginManager()
auth.define_tables(migrate=False)
auth.define_tables(username=False, signature=False)
db.define_table('nsksystem',
Field('email_id', db.auth_user,length=512, label = 'Email ID'),
Field('nskname', length=128, default='', label = 'Machine Name'),
Field('nskpassword', 'password', length=512,readable=False, label='Machine Password'),
Field('confirmnskpassword', 'password', length=512,readable=False, label='Confirm Machine Password'),
Field('nreleaseid',length=128, default='',label = 'Release'),
Field('isCordinator','boolean',default='', label = 'Is Co-ordinator'))
db.nsksystem.email_id.requires = IS_NOT_EMPTY(error_message=auth.messages.is_empty)
db.nsksystem.email_id.requires = IS_IN_DB(db,'auth_user.email','%(email)s')
db.nsksystem.nreleaseid.requires = IS_NOT_EMPTY(error_message=auth.messages.is_empty)
db.nsksystem.nskname.requires = IS_NOT_EMPTY(error_message=auth.messages.is_empty)
db.nsksystem.confirmnskpassword.requires = IS_EXPR('value==%s' % repr(request.vars.get('nskpassword', None)),error_message='Passwords do not match')
db.nsksystem.isCordinator.requires=IS_NOT_IN_DB(db(db.nsksystem.nreleaseid == request.vars.nreleaseid), 'nsksystem.isCordinator', error_message='Co-ordinator Already exist for specified release')
控制器
def uregistration():
form=SQLFORM(db.auth_user)
if form.process().accepted:
response.flash = 'User is registered. Redirecting to machine registration'
redirect(URL('mregistration'))
elif form.errors:
response.flash = 'Form has errors'
else:
response.flash = 'Please fill out the form'
return dict(form=form)
def mregistration():
form=SQLFORM(db.nsksystem)
if form.process().accepted:
response.flash = 'Machine is registered to user. Please go to Login page.'
redirect(URL('index'))
elif form.errors:
response.flash = 'Form has errors'
else:
response.flash = 'Please fill out the form'
return dict(form=form)
成功注册后,我被引导至机器注册 URL。我不得不从下拉列表中选择 select 电子邮件 ID。我在注册时提供的这个电子邮件 ID。
提交后出现此错误。
回溯
Traceback (most recent call last):
File "gluon/restricted.py", line 224, in restricted
File "C:/web2py/applications/click/controllers/default.py", line 63, in <module>
File "gluon/globals.py", line 393, in <lambda>
File "C:/web2py/applications/click/controllers/default.py", line 30, in mregistration
if form.process().accepted:
File "gluon/html.py", line 2303, in process
File "gluon/html.py", line 2240, in validate
File "gluon/sqlhtml.py", line 1677, in accepts
File "gluon/dal/objects.py", line 724, in insert
File "gluon/dal/adapters/base.py", line 715, in insert
IntegrityError: foreign key constraint failed
Error snapshot
<class 'sqlite3.IntegrityError'>(foreign key constraint failed)
Function argument list
(self=<gluon.dal.adapters.sqlite.SQLiteAdapter object>,
table=<Table nsksystem (id,email_id,nskname,nskpassword,confirmnskpassword,nreleaseid,isCordinator)>,
fields=[(<gluon.dal.objects.Field object>, '1234'),
(<gluon.dal.objects.Field object>, 'yennae.ind.codefactory.com'),
(<gluon.dal.objects.Field object>, 0),
(<gluon.dal.objects.Field object>, True),
(<gluon.dal.objects.Field object>, 'AAA'),
(<gluon.dal.objects.Field object>, '1234')])
此外,我没有看到我的电子邮件 ID 显示在票证中显示的函数参数列表中。
我无法关闭约束,因为这是一项要求。
接下来,即使我刷新 db.auth_user,也会自动添加 10 条记录。我怎样才能阻止它。
db.nsksystem.email_id
被定义为对 db.auth_user
table 的引用,这意味着它必须存储来自 db.auth_user
的记录 ID(这是一个整数),而不是一个电子邮件地址。因此,您定义的验证器不正确:
db.nsksystem.email_id.requires = IS_IN_DB(db, 'auth_user.email', '%(email)s')
应该改为:
db.nsksystem.email_id.requires = IS_IN_DB(db, 'auth_user.id', '%(email)s')
事实上,没有理由明确定义该验证器,因为它是引用字段的默认验证器。 (请注意,HTML select 小部件将显示电子邮件地址列表,但提交和存储的实际值将是与电子邮件地址关联的 auth_user
记录 ID。)
其他几个问题:
不要调用 auth.define_tables()
两次(并且不要用 migrate=False
调用它,除非 table 已经根据需要定义)。
不要两次设置字段的 requires
属性,因为第二次会简单地覆盖第一次。此外,如果还有 IS_IN_DB
验证器,则无需设置 IS_NOT_EMPTY
验证器,因为后者无论如何都不允许空值。
我知道这是第 n 次在论坛上提出这个问题了,但是我需要你的帮助,因为我无法弄清楚我做错了什么。
型号
db = DAL('sqlite://storage.sqlite',pool_size=1,check_reserved=['all'])
auth = Auth(db)
service = Service()
plugins = PluginManager()
auth.define_tables(migrate=False)
auth.define_tables(username=False, signature=False)
db.define_table('nsksystem',
Field('email_id', db.auth_user,length=512, label = 'Email ID'),
Field('nskname', length=128, default='', label = 'Machine Name'),
Field('nskpassword', 'password', length=512,readable=False, label='Machine Password'),
Field('confirmnskpassword', 'password', length=512,readable=False, label='Confirm Machine Password'),
Field('nreleaseid',length=128, default='',label = 'Release'),
Field('isCordinator','boolean',default='', label = 'Is Co-ordinator'))
db.nsksystem.email_id.requires = IS_NOT_EMPTY(error_message=auth.messages.is_empty)
db.nsksystem.email_id.requires = IS_IN_DB(db,'auth_user.email','%(email)s')
db.nsksystem.nreleaseid.requires = IS_NOT_EMPTY(error_message=auth.messages.is_empty)
db.nsksystem.nskname.requires = IS_NOT_EMPTY(error_message=auth.messages.is_empty)
db.nsksystem.confirmnskpassword.requires = IS_EXPR('value==%s' % repr(request.vars.get('nskpassword', None)),error_message='Passwords do not match')
db.nsksystem.isCordinator.requires=IS_NOT_IN_DB(db(db.nsksystem.nreleaseid == request.vars.nreleaseid), 'nsksystem.isCordinator', error_message='Co-ordinator Already exist for specified release')
控制器
def uregistration():
form=SQLFORM(db.auth_user)
if form.process().accepted:
response.flash = 'User is registered. Redirecting to machine registration'
redirect(URL('mregistration'))
elif form.errors:
response.flash = 'Form has errors'
else:
response.flash = 'Please fill out the form'
return dict(form=form)
def mregistration():
form=SQLFORM(db.nsksystem)
if form.process().accepted:
response.flash = 'Machine is registered to user. Please go to Login page.'
redirect(URL('index'))
elif form.errors:
response.flash = 'Form has errors'
else:
response.flash = 'Please fill out the form'
return dict(form=form)
成功注册后,我被引导至机器注册 URL。我不得不从下拉列表中选择 select 电子邮件 ID。我在注册时提供的这个电子邮件 ID。 提交后出现此错误。
回溯
Traceback (most recent call last):
File "gluon/restricted.py", line 224, in restricted
File "C:/web2py/applications/click/controllers/default.py", line 63, in <module>
File "gluon/globals.py", line 393, in <lambda>
File "C:/web2py/applications/click/controllers/default.py", line 30, in mregistration
if form.process().accepted:
File "gluon/html.py", line 2303, in process
File "gluon/html.py", line 2240, in validate
File "gluon/sqlhtml.py", line 1677, in accepts
File "gluon/dal/objects.py", line 724, in insert
File "gluon/dal/adapters/base.py", line 715, in insert
IntegrityError: foreign key constraint failed
Error snapshot
<class 'sqlite3.IntegrityError'>(foreign key constraint failed)
Function argument list
(self=<gluon.dal.adapters.sqlite.SQLiteAdapter object>,
table=<Table nsksystem (id,email_id,nskname,nskpassword,confirmnskpassword,nreleaseid,isCordinator)>,
fields=[(<gluon.dal.objects.Field object>, '1234'),
(<gluon.dal.objects.Field object>, 'yennae.ind.codefactory.com'),
(<gluon.dal.objects.Field object>, 0),
(<gluon.dal.objects.Field object>, True),
(<gluon.dal.objects.Field object>, 'AAA'),
(<gluon.dal.objects.Field object>, '1234')])
此外,我没有看到我的电子邮件 ID 显示在票证中显示的函数参数列表中。 我无法关闭约束,因为这是一项要求。
接下来,即使我刷新 db.auth_user,也会自动添加 10 条记录。我怎样才能阻止它。
db.nsksystem.email_id
被定义为对 db.auth_user
table 的引用,这意味着它必须存储来自 db.auth_user
的记录 ID(这是一个整数),而不是一个电子邮件地址。因此,您定义的验证器不正确:
db.nsksystem.email_id.requires = IS_IN_DB(db, 'auth_user.email', '%(email)s')
应该改为:
db.nsksystem.email_id.requires = IS_IN_DB(db, 'auth_user.id', '%(email)s')
事实上,没有理由明确定义该验证器,因为它是引用字段的默认验证器。 (请注意,HTML select 小部件将显示电子邮件地址列表,但提交和存储的实际值将是与电子邮件地址关联的 auth_user
记录 ID。)
其他几个问题:
不要调用 auth.define_tables()
两次(并且不要用 migrate=False
调用它,除非 table 已经根据需要定义)。
不要两次设置字段的 requires
属性,因为第二次会简单地覆盖第一次。此外,如果还有 IS_IN_DB
验证器,则无需设置 IS_NOT_EMPTY
验证器,因为后者无论如何都不允许空值。