Implementing constraints in couchrest-model

Skip to first unread message

Sam Wilder

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

Oct 3, 2011, 5:08:20 AM10/3/11

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,


You received this message because you are subscribed to the Google Groups "CouchRest" group.
To post to this group, send email to
To unsubscribe from this group, send email to
For more options, visit this group at

Reply all
Reply to author
0 new messages