Love this post, great exploration of the nuances of uniqueness validations.
I came here to mention that there is also the https://github.com/toptal/database_validations gem which allows you to handle uniqueness validations in Rails in a similar manner to Ecto.
One note when using this for form validation. If you have a multi-column unique index Phoenix will show the error for the field defined first in the constraint in the schema definition. This has tripped me up a couple of times for user input where the constraint is something like tag+post_id. If your form just has the tag input and you put post_id as the first item in the constraint the changeset will be invalid but no error will show.
4 comments
[ 3.4 ms ] story [ 20.2 ms ] threadI came here to mention that there is also the https://github.com/toptal/database_validations gem which allows you to handle uniqueness validations in Rails in a similar manner to Ecto.
It will still work exactly as expected if you provide the index name manually, even if it's not the same order as the constraint in the DB.