Hi friends, first off, thanks to every contributor here - the web2py
project is a godsend.
So basically, I'm getting two different problems in two separate table
insertions, which occurs in the controller.
When I manually insert into the db (sqlite atm), I get an IntegrityError.
- My controller:
@auth.requires_login()
def new():
response.menu = MENU
form = SQLFORM(db.text_messages, submit_button='Send!',
formstyle='bootstrap3_inline')
if form.validate():
session.flash = 'Congratulations!'
form.vars.phone_number_id = int(form.vars.phone_number_id)
payload = dict(**form.vars)
payload['user_id'] = auth.user.id
id = db.text_messages.insert(**payload)
session.formvars = form.vars
redirect(URL('dashboard', 'index'))
elif form.errors:
response.flash = form.errors.number
return dict(form=form)
-
- My model (db.py):
## define table for supported countries
db.define_table('countries',
Field('name', writable=False),
Field('iso', writable=False, readable=False),
redefine=True)
db.define_table('numbers',
Field('country_id', db.countries),
Field('phone_number', 'string',
unique=True),
Field('user_id', db.auth_user,
#default=auth.user_id,
writable=False, readable=False),
redefine=True)
db.numbers.country_id.requires = IS_IN_DB(db, 'countries.id', '%(name)s',
zero=T('choose one'),
error_message="Please select from the
list")
db.numbers.phone_number.requires = [IS_NOT_EMPTY(),
IS_NOT_IN_DB(db, 'numbers.phone_number')]
db.define_table('text_messages',
Field('text_message', 'string', requires=IS_NOT_EMPTY()),
Field('phone_number_id', 'reference numbers'),
Field('recipients', 'list:string', requires=IS_NOT_EMPTY()),
Field('user_id', 'reference auth_user',
#default=auth.user.id,
writable=False, readable=False),
redefine=True)
db.text_messages.phone_number_id.requires = IS_IN_DB(db, 'numbers.id',
'%(phone_number)s',
zero=T('choose one'),
error_message="Please select from the
-
*The error:*
-
*File
C:\Users\rodrigo\PycharmProjects\TextMate\web2py\gluon\packages\dal\pydal\adapters\base.py
in insert at line 739* code arguments variables
Function argument list
(self=<pydal.adapters.sqlite.SQLiteAdapter object>, table=<Table
text_messages (id,text_message,recipients,user_id,phone_number_id)>,
fields=[(<pydal.objects.Field object>, 'hi lol'), (<pydal.objects.Field
object>, 41), (<pydal.objects.Field object>, 1), (<pydal.objects.Field
object>, ['1515151'])])
Code listing
734.
735.
736.
737.
738.
739.
740.
741.
742.
743.
self.execute(query)
except Exception:
e = sys.exc_info()[1]
if hasattr(table,'_on_insert_error'):
return table._on_insert_error(table,fields,e)
raise e
if hasattr(table, '_primarykey'):
mydict = dict([(k[0].name, k[1]) for k in fields if k[0].name in
table._primarykey])
if mydict != {}:
return mydict
VariableseIntegrityError('foreign key constraint failed',)
- ... And when I the controller is changed to this:
@auth.requires_login()
def new():
response.menu = MENU
form = SQLFORM(db.text_messages, submit_button='Send!',
formstyle='bootstrap3_inline')
if form.process().accepted:
response.flash = 'form accepted'
elif form.errors:
response.flash = 'form has errors'
return dict(form=form)
The insertion works but the text_messages.user_id field is assigned the value
of None. I'm not sure where my problem is at :/
If anyone spots my error or has any advice, I'm all ears :) Thanks in advance!
--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.