On Thursday, April 16, 2015 at 7:01:22 AM UTC-7, Anthony wrote:
>
> A followup question. The reason I need my clumsy 
>>>> customized_email_verification() in the first place, is because I seemingly 
>>>> have to do db.commit() BEFORE the time consuming email sending job. 
>>>> Otherwise I saw some randomly duplicated records in my auth_user table. I 
>>>> don't know the exact reason but my guess is, the tools.py's 
>>>> define_tables() 
>>>> uses IS_NOT_IN_DB() validators which only work in web2py level, but does 
>>>> NOT define any "unique=True" in the DB level. Is this considered as a 
>>>> defect?
>>>>
>>>
>>> It's not a defect, just something you have to handle properly in your 
>>> code. You can separately set the unique=True argument when creating the 
>>> model, and that will be enforced by the database, 
>>>
>>
>> I know I can always define my OTHER tables properly. But now we are 
>> talking about the web2py built-in auth_user table. Do I have to redefine 
>> them in my models/db.py EVERY TIME I start a new project? That doesn't 
>> sound right. Is there any reason we don't want to do that inside 
>> gluon/tools.py?
>>
>
> I think you should be able to set the "unique" attribute of individual 
> fields after the table has been defined, so you shouldn't need to re-define 
> the entire table. I don't think we want to set that attribute in tools.py 
> because we would then need a mechanism for catching database errors that 
> might be generated.
>
> Anthony
>

Thanks for your hint, Anthony. I did not realize that I can set that 
"unique" attribute after db.define_tables(). Will definitely try to do that 
in my projects.

That said, I still believe this should be a default behavior for an 
out-of-box web2py app. Reasons below.

1. To address your concern about "an attempted duplicate entry will end up 
raising an exception and returning a 500 response unless you catch and 
handle the error.", we could probably add something like this snippet at 
the (nearly) end of the "if form.accepts(...)" branch inside that 
register() in tools.py:

    try:
        db.commit()
    except Exception as e:
        db.rollback()
        response.flash = "Something is wrong. %s" % e  # It is better than 
nothing

2. Even without above snippet, in our context I would say a rare 
500-response-but-it-would-be-gone-if-you-simply-retry is (arguably?) still 
better than, silently creating duplicated user accounts which clearly 
violates the universal expectation of such a fundamental db scheme 
assumption.

-- 
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 web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to