On Friday, January 22, 2016 at 1:59:01 PM UTC-5, Pierre wrote:
>
> it's option (a) fail silently am trying to achieve
> given all these elements and to keep things as simple as possible I'll use
> update_or_insert(first_ref=ref_a
> , second_ref=ref_b) with 2 separated ref fields first_ref, second_ref
>
That's fine as long as those are the only two fields in the table.
Otherwise, you need to use first_ref and second_ref as keys, and then
insert the other fields as well. The problem with that approach is that
instead of failing upon encountering a duplicate, it will simply overwrite
the matched record. Is that what you want?
A simpler option might be a _before_insert callback:
db.mytable._before_insert.append(lambda f: not db((db.mytable.first_ref == f
['first_ref']) &
(db.mytable.second_ref ==
f['second_ref'])).isempty())
The above callback will return True when a matching record is found, and
when a _before_* callback returns True, the operation is (silently)
aborted. The nice thing about this approach is that it is part of the table
definition, so it will work from any code where you do an insert.
> As for setting the two columns to be unique directly in postgre. Is this
> really useful ? isn't it redundant with what web2py already does with the
> above command
>
It's not really necessary if you are doing all inserts via web2py (and you
have implemented the proper checks in all places where inserts happen), but
it couldn't hurt as a fail-safe, just to make sure there is no way for a
duplicate to get in the database via any means (as the database itself will
enforce the constraint).
> UUID ? no :))
>
Not sure what the upside would be given the other options.
Anthony
--
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.