Of course, you should follow the web2py principle - "everything should have
a default behavior". Default the priority to the lowest possible, or the
most frequently used value.
On Monday, December 10, 2012 3:33:15 PM UTC-7, Derek wrote:
>
> The problem with IS_IN_DB is that it creates a foreign key constraint - It
> appears you have 'zero="choose one"' - you're adding a foreign key that
> isn't actually in the database. I'd suggest that you add "choose one" to
> the "requirement_priorities" table, and add in validation logic to throw an
> error when you try to pick it.
>
> On Tuesday, November 27, 2012 2:11:51 PM UTC-7, israelord wrote:
>>
>> Hi there! and greetings from Caracas - Venezuela,
>>
>> I started using web2py recently thanks to a project at the office, I find
>> it very easy
>> to use and I'm actualy liking it.
>>
>> The thing is that I defined a table, with a *reference* field which
>> accepts null values and,
>> for some reason, when it's null, I get the following integrity error:
>>
>> <class 'psycopg2.IntegrityError'> insert or update on table
>> "requirements" violates foreign key constraint
>> "requirements_requirement_priority_id_fkey" DETAIL: Key
>> (requirement_priority_id)=(0) is not present in table
>> "requirement_priorities".
>>
>> I've been reading for a while, I haven't found so much information, I
>> found someone who had the same issue[0] and someone who tried to help[1],
>> but I tried what he told and it didn't work for me.
>>
>> Here is my table definition:
>>
>> db.define_table(t.requirements
>> ,Field('num_invoice', 'string', default = '', label =
>> T('Invoice Number'), \
>> required = False)
>> ,Field('total_invoice', 'decimal(10,4)', default = 0, label =
>> T('Invoice Total'), \
>> required = False)
>> ,Field('requested_payment', 'decimal(10,4)', default = 0,
>> label = T('Requested Payment'), \
>> required = True, notnull = True)
>> ,Field('approved_payment', 'decimal(10,4)', default = 0,
>> label = T('Approved Payment'), \
>> required = True, notnull = True)
>> ,Field('description', 'string', length = 1024,default = '',
>> label = T('Description'), \
>> required = True, notnull = True, widget =
>> SQLFORM.widgets.text.widget)
>> ,Field('reminder_call_count','integer', default = 0, label =
>> T('Reminder Call Count'), \
>> required = True, notnull = True)
>> ,Field('status_id','reference %s' % t.status, default = 1,
>> label = T('Status'), \
>> required = True, notnull = True)
>> ,Field('requirement_type_id', 'reference %s' %
>> t.requirement_types, default = 1, label = T('Requirement Type'), \
>> required = True, notnull = True)
>> ,Field('requirement_priority_id', 'reference %s' %
>> t.requirement_priorities, label=T("Requirement Priority"))
>> ,auth.signature)
>>
>> The problematic field is "requirement_priority_id", and this is how I
>> defined the validators for this field
>>
>> db[t.requirements].requirement_priority_id.required =
>> IS_NULL_OR(IS_IN_DB(db, '%s.name' % t.requirement_priorities, \
>> zero=T('Choose One')))
>>
>> I also tried adding required=False, notnull=False and default=None in the
>> Field definition and I still had the same problem.
>>
>> I searched and tried to debug web2py code, searching for any
>> safe_int(value) or something like that, but I got nothing,
>> I searched on the files html.py, sqlhtml.py and in the FORM class, on the
>> validate() and process() methods.
>>
>> Can somebody give me a light on this?
>>
>> Thank you in advance for your time.
>>
>> [0] http://www.mail-archive.com/[email protected]/msg67428.html
>> [1] http://www.mail-archive.com/[email protected]/msg67745.html
>>
>
--