mmmrrr

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?

Showing Posts 1 to 10

sodapopcan

sodapopcan

There is the contraint/3 function.

cevado

cevado

create table("table_name") do
  add :my_unique_field, :integer
end

create unique_index("table_name", [:my_unique_field])

for reference

mmmrrr

mmmrrr OP

If I understand it correctly, the constraint/3 function is just for check or exclusion constraints. 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

mmmrrr OP

Thanks! This is a unique_index though, which is distinct from a unique constraint on a table column :slight_smile:

cevado

cevado

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

Adding a unique constraint will automatically create a unique B-tree index on the column or group of columns listed in the constraint. A uniqueness restriction covering only some rows cannot be written as a unique constraint, but it is possible to enforce such a restriction by creating a unique partial index.
PostgreSQL: Documentation: 18: 5.5. Constraints

sbuttgereit

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:

PostgreSQL automatically creates a unique index when a unique constraint or primary key is defined for a table. The index covers the columns that make up the primary key or unique constraint (a multicolumn index, if appropriate), and is the mechanism that enforces the constraint.

It is valid to create the index without the constraint.

LostKobrakai

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,2 to 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

cevado

if i understand correctly all uniqueness checks are validated when the transaction is commited, that is the same as deferring a constraint.

If a conflicting valid row has been deleted by the current transaction, it’s okay. (In particular, since an UPDATE always deletes the old row version before inserting the new version, this will allow an UPDATE on a row without changing the key.)
PostgreSQL: Documentation: 18: 63.5. Index Uniqueness Checks

sbuttgereit

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

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.

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
bradley
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
unaware8150
Hello folks! So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
ChrisAmelia
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication): toke...
New
dillonoconnor
Is there any way to avoid the Hologram compiler running when using iex? It seems like the front-end code could potentially be disregarded...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews