mmmrrr
How to set a UNIQUE column constraint in PostgreSQL
I’m currently trying to mimick a database migration from another system that needs to live in ecto in the future.
I’m trying to implement it as faithful as possible and due to this reason I’d like to set a UNIQUE constraint on the column and not just a unique index.
Until now I haven’t found a way to specify extra constraints. The resulting SQL should be something like
CREATE TABLE (
my_unique_field integer UNIQUE
)
It is probably not adding much but since I don’t want to take any chances I’d want this added.
I know that I can simply call raw SQL to alter the tables afterwards, but it would be nice if it was possible to specify this upfront in the add call, for example like this:
create table(...) do
add(:my_unique_field, :integer, unique: true)
end
Any pointers if this is possible or why it is not?
Trending in Questions
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated!
To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead.
Sta...
New
Hello !
We want new/edit form pages to POST/PUT to their own URL rather than the resources REST defaults (post /things, put /things/:id)...
New
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app?
Looking for hints regarding:
Addi...
New
Hi all, I wanted to ask how the community is dealing with post-release steps.
Today we have Ecto migrations, which make sure that the db...
New
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New
Other Trending Topics
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #elixirconf-us
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #performance
- #security










First 10 of 13 Posts!
sodapopcan
There is the
contraint/3function.cevado
for reference
mmmrrr
If I understand it correctly, the
constraint/3function is just forcheckorexclusionconstraints. You cannot e. g. say:create constraint(:my_table, unique: :my_unique_field)So the check constraint will yield a
ALTER TABLE ... ADD CONSTRAINT ... CHECK (...)which is not what I’m after. But in theory this would be the correct function to implement such a thing.mmmrrr
Thanks! This is a
unique_indexthough, which is distinct from aunique constrainton a table columncevado
afaik they’re the same thing at least on regular sql databases(i’m not sure about timescale and other “sql-like” databases with mods). but on regular sql db unique constraint on a column is achieved by a unique index.
just for reference from postgres
sbuttgereit
From the PostgreSQL perspective this is a distinction without a real difference. The unique constraint is implemented via the unique index.
Per the docs:
It is valid to create the index without the constraint.
LostKobrakai
To all the people stating that unique indexes and constraints are the same: This is not true.
If all you need are the features and functionality of a unique index the difference does not matter, but if you want to use the unique constraint with all the features of constraints – like being able to defer their validation – the difference does indeed matter.
@mmmrrr you can use
execute/1,2to add the constraint with raw sql after having created the table. Ecto doesn’t need to have explicit api for any and all things your db supports.Edit: Example of that can be found in Unnest for runtime sorted results | Benjamin Milde (migration is in the code comments)
cevado
if i understand correctly all uniqueness checks are validated when the transaction is commited, that is the same as deferring a constraint.
sbuttgereit
Fair enough, though uniqueness deferral is in my experience not common (yes, it absolutely does happen). Absent any specific need for the deferral and I’m just as likely to go for the index… because while you cannot defer an index, you can’t build a constraint backed by an index concurrently… so something gained, something lost… naturally there are caveats to all of this.
However, if you just need uniqueness… a unique index alone is perfectly valid.
LostKobrakai
That’s not correct. You get the error for uniqueness validation immediately on the command creating the violation. Delete and insert has no conflict. Allowing two rows to temporarily share a unique column value within a transaction needs a deferred constraint.
Last Post!
mmmrrr
Thank you all for the great discussion and insights! It’s much appreciated
@LostKobrakai I agree that Ecto shouldn’t support every feature of every storage engine under the sun but in this case a little syntax sugar would have been nice
actually I already did use execute as you suggested but it felt a little heavy handed for something seemingly simple - hence my question.
@sbuttgereit thank you so much for the in depth analysis. This was a fantastic answer - and the kind of answer why I adore this community. You all are really an asset to this ecosystem!