Implementing constraints in couchrest-model

8 views
Skip to first unread message

Sam Wilder

unread,
Oct 1, 2011, 8:09:33 PM10/1/11
to CouchRest
What is the best practice in CouchDB domain for implementing something
like a unique constraint. I could do this with the _id , but what if
I want to implemnet it on multiple fields, not necesarily as a
composite, but even separately. Say for e.g. I want email ID and the
username both to be unique, just to point out an example.
Is there a best practice around doing thsi without using the _id field.

Samuel Lown

unread,
Oct 3, 2011, 5:08:20 AM10/3/11
to couc...@googlegroups.com

IMHO, the current best practice is to avoid it if at all possible :-)

By design you data could be coming from anywhere, at any point in time (replication) so unique constraints are not really practical, even with the _id field.

Having said that, couchrest_model's validates_uniqueness_of should work for most simple cases where there is a low probability of collision (two users with the same email address is not likely.)


Its not documented yet, but you can also do "scoped" validations:

validates_uniqueness_of :title, :scope => :parent_id

This would use the view "by_parent_id_and_title" to check for existing matches.

Hope that helps,

Cheers,
sam



--
You received this message because you are subscribed to the Google Groups "CouchRest" group.
To post to this group, send email to couc...@googlegroups.com.
To unsubscribe from this group, send email to couchrest+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/couchrest?hl=en.




--
www.samlown.com
www.autofiscal.com
www.planetaki.com/sam
Reply all
Reply to author
Forward
0 new messages