crud.create(db.table) issues error ticket

6 views
Skip to first unread message

annet

unread,
Nov 17, 2010, 3:35:13 AM11/17/10
to web2py-users
I set up a mock app to help someone solve a problem.

In db.py I defined these two tables after all the mail, auth and crud
stuff:

db.define_table('mockbio',

Field('unique_id',type='integer',unique=True,writable=False,readable=True),
Field('first_name',length=24),
Field('last_name',length=72),
migrate='mockbio.table')

db.mockbio.unique_id.default=auth.user_id
db.mockbio.unique_id.requires=IS_NOT_IN_DB(db,db.bio.unique_id)
db.mockbio.unique_id.label=T('Unique_id')


Using web2py's administrative interface I am able to enter a record
for each user I created, when I enter a second record for a user I get
an error on the unique_id field: value already in database or empty.



I also defined two functions in default.py:

@auth.requires_login()
def create_mockbio_first():
form=crud.create(table=db.mockbio)
return dict(form=form)


@auth.requires_login()
def create_mockbio_second():
form=SQLFORM(db.mockbio)
if form.accepts(request.vars, session):
session.flash = 'form accepted'
elif form.errors:
response.flash = 'form has errors'
else:
response.flash = 'please fill the form'
return dict(form=form)


When I expose the functions, I can create a record for the logged in
user once, when I try to create a second record for the same user an
error ticket is issued: IntegrityError: column unique_id is not unique

Why do I get an error ticket instead of an error message in the form?


Kind regards,

Annet

DenesL

unread,
Nov 17, 2010, 6:36:51 AM11/17/10
to web2py-users

Hi Annet,

because of the writable=False on unique_id.

When you do that then the field is not included in the form and it is
not checked on a request, so the form is accepted based on the other
fields and the error is the result of the enforcement of unique=True
at the DB level on the auto insert.

Denes.

annet

unread,
Nov 17, 2010, 10:09:36 AM11/17/10
to web2py-users
Hi Denes,

Thanks for your explanation. The mock app's behaviour made me worry
about my own app's behaviour, however, I chose a different approach,
and know I understand why I won't encounter this problem.


Kind regards,

Annet.
Reply all
Reply to author
Forward
0 new messages