mmmrrr
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
Hey guys,
I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly
Do you guys have any suggestions what is the best prac...
New
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello!
Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app.
I creat...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
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
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
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
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #ai
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
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.