Reputation: 1167
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.
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. I had to select email id from the list of a drop down. This email ID i had given while registration. 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. 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. How can i stop this.
Upvotes: 0
Views: 3502
Reputation: 25536
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. 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.)
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).
Don't set the requires
attribute of a field twice, as the second one will simply overwrite the first. 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.
Upvotes: 3