简体   繁体   English

web2py:sqlite3.IntegrityError'>(外键约束失败)

[英]web2py: sqlite3.IntegrityError'>(foreign key constraint failed)

I know this is nth time this question is being asked in the forum,but please i need your help as i am not able to figure out what i am doing wrong. 我知道这是在论坛中第n次问这个问题了,但是由于我无法弄清楚我在做什么错,请您需要我的帮助。

Model 模型

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')

Controller 控制者

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)

After i did a successful registration I was directed to Machine registration URL. 成功注册后,我被定向到机器注册URL。 I had to select email id from the list of a drop down. 我必须从下拉列表中选择电子邮件ID。 This email ID i had given while registration. 我在注册时提供的电子邮件ID。 After i submit i get this error. 我提交后,我得到此错误。

Traceback 追溯

  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')])

And moreover i dont see my email id being displayed in the Function argument list, that is displayed in the ticket. 而且,我没有看到我的电子邮件ID显示在故障单中显示的Function参数列表中。 I cant turn the constraint off as it is a requirement. 我无法关闭约束,因为这是一项要求。

And next thing even though i flush my db.auth_user , 10 records getadded automatically. 接下来的事情即使我刷新了db.auth_user,也会自动添加10条记录。 How can i stop this. 我该如何阻止。

db.nsksystem.email_id is defined as a reference to the db.auth_user table, which means it must store a record ID from db.auth_user (which is an integer), not an email address. db.nsksystem.email_id定义为对db.auth_user表的引用,这意味着它必须存储db.auth_user的记录ID(是整数),而不是电子邮件地址。 So, the validator you have defined is incorrect: 因此,您定义的验证器不正确:

db.nsksystem.email_id.requires = IS_IN_DB(db, 'auth_user.email', '%(email)s')

It should instead be: 相反,它应该是:

db.nsksystem.email_id.requires = IS_IN_DB(db, 'auth_user.id', '%(email)s')

In fact, there is no reason to explicitly define that validator, as it is the default validator for a reference field. 实际上,没有理由明确定义该验证器,因为它是参考字段的默认验证器。 (Note, the HTML select widget will display a list of email addresses, but the actual value submitted and stored will be the auth_user record ID associated with the email address.) (请注意,HTML select小部件将显示电子邮件地址列表,但是提交和存储的实际值将是与电子邮件地址关联的auth_user记录ID。)

A couple other problems: 其他几个问题:

Don't call auth.define_tables() twice (and don't call it with migrate=False unless the tables have already been defined as you need them). 不要两次调用auth.define_tables() (除非您已经根据需要定义了表,否则不要使用auth.define_tables() migrate=False调用它)。

Don't set the requires attribute of a field twice, as the second one will simply overwrite the first. 不要将字段的requires属性设置两次,因为第二个将简单地覆盖第一个。 Also, there is no need to set an IS_NOT_EMPTY validator if there is also an IS_IN_DB validator, as the latter does not allow empty values anyway. 另外,如果还有IS_NOT_EMPTY验证器,则无需设置IS_NOT_EMPTY验证器,因为后者IS_IN_DB不允许空值。

暂无
暂无

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

相关问题 如何修复Web2Py“ <class 'sqlite3.IntegrityError'> (外键约束失败)“? - How to fix Web2Py “<class '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.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 使用 flask sqlalchemy.exc.IntegrityError 时出错:(sqlite3.IntegrityError) UNIQUE 约束失败 - Error when working with flask sqlalchemy.exc.IntegrityError: (sqlite3.IntegrityError) UNIQUE constraint failed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM