I tis also possible to use _before_insert before form process 
: 
http://stackoverflow.com/questions/8054665/multi-column-unique-constraint-with-web2py/23738715#23738715



On Sunday, 29 June 2014 02:16:14 UTC+1, Daniel Lafrance wrote:
>
> Thanks M. Di Pierro
>
> I was not expecting an answer so fast.
>
> I will give that a try but I can not remove the unique clause from the 
> DBMS because other apps (PHP and PERL) also have access to it.
>
> Regard's
>
> D. Lafrance
>
> Le vendredi 27 juin 2014 02:20:26 UTC-4, Massimo Di Pierro a écrit :
>>
>> I would not do it that way. There are two places to enforce uniqueness: 
>> at the database level and at the form level.
>>
>> To enforce is at the database level you can manually do 
>>
>> create table sometable (
>>     a_field varchar(10),
>>     b_field timestamp default date(now()),
>>     unique(a_field, b_field)
>> );
>>
>> and in web2py:
>>
>> db.define_table('sometable',
>>                 Field('a_field', 'string', length=10),
>>                 Field('b_field','datetime', default=request.now),
>>                 migrate=False
>> )
>>
>> so web2py will take the table as in database. This still will not enforce 
>> uniqueness in forms.
>>
>> To enforce uniqueness in forms, the problem is, you need to specify how 
>> is the error to be reported. Let's say you want the b field to report the 
>> error. You can do this in the action, before SQLFORM...
>>
>> def index():
>>      if request.post_vars.b_field:
>>          db.sometable.b_field.requires = 
>> IS_NOT_IN_DB(db(db.sometable.a_field==
>> request.post_vars.a_field),'b_field')
>>      form = SQLFORM(db.sometable).process()
>>      ....
>>
>>
>>
>>
>>
>> On Thursday, 26 June 2014 12:32:17 UTC-5, Daniel Lafrance wrote:
>>>
>>> Hi gang
>>>
>>> I am quite new to web2py and i am confronted to the following lack of my 
>>> own knowledge.
>>>
>>> In other DB engine one can write :
>>>
>>> create table sometable (
>>>     a_field varchar(10),
>>>     b_field timestamp default date(now()),
>>>     unique(a_field, b_field)
>>> );
>>>
>>> wich I have reproduce in web2py using the following syntax:
>>>
>>> # coding: utf8
>>> db.define_table('sometable',
>>>                 Field('a_field', 'string', length=10),
>>>                 Field('b_field','datetime', default=request.now),
>>>                 Field('unique_fields','text',compute=lambda s: 
>>> str(s.a_field) + str(s.b_field), unique=True)
>>>                 )
>>> db.sometable.requires = IS_NOT_IN_DB(db, 'sometable.unique_fields')
>>>
>>> Written as is it works and I get a ticket when I try to create duplicate.
>>>
>>> My question is : 
>>>
>>> In some posts it is being said not to put the "unique=True" for the 
>>> unique field.
>>> I have tried it, (after deleting all databases files created by web2py) 
>>> and the IS_NOT_IN_DB clause does not seem to work.
>>>
>>> Any idea of what I am doing wrong ?
>>>
>>> Regard's
>>>
>>> Daniel L
>>>     
>>>
>>>

-- 
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.

Reply via email to