Complex Unique Constraints with PostgreSQL Triggers in Ecto
http://johnwilger.com/2020/02/16/ecto-custom-unique-constraint-trigger.html
I recently needed to enforce a database constraint similar in spirit to a unique index, however the criteria for what should be considered “unique” was more complex than what a simple unique index in PostgreSQL would be able to deal with. Knowing that Ecto’s unique constraint validation works by intercepting an error raised by the database, I set out to see if I could implement the complex unique constraint logic in the database and still be able to use the Ecto.Changeset.unique_constraint/3
validation without needing to modify any Elixir code.
Read next Simple Phoenix LiveView App: Socket Session info, CSRF & .leex