dli

dli

How can I specify the order of a composite primary key in an Ecto migration?

I am creating a schema where the order of the column definitions is relevant due to optimum memory alignment. The table needs to hold a very large number of rows, hence the additional pain here.

When I add columns with primary: true, the resulting composite primary key is defined like this: PRIMARY KEY (timestamp, record_id, confirmed)

create table(:foo, primary_key: false) do
  add :timestamp, :timestamptz, null: false, primary: true
  add :bar, :real, null: false
  add :baz, :real, null: false
  # ... other columns
  add :record_id, :smallint, null: false, primary: true
  add :confirmed, :boolean, null: false, primary: true
end

However, I need to modify this order and generate a PRIMARY KEY (record_id, timestamp, confirmed) to better suit the table partitioning. The order of the columns in the table itself must remain as it is.

Right now, the only way I see is to generate the table without any primary: true columns, then execute a custom SQL statement to add the properly ordered primary key.

Did I miss an Ecto feature that does this for me?

Marked As Solved

garrison

garrison

It uses the order in which you define the columns. It’s unfortunately a bit unintuitive for people used to real SQL :slight_smile:

Also Liked

dli

dli

It uses the order in which you define the columns. It’s unfortunately a bit unintuitive for people used to real SQL :slight_smile:

Damn, that’s what I thought :wink:

Something like create primary_key(...) would be great, similar to create index(...).

in Postgres, you do not need null: false on primary key columns

I know, I just use this to clarify the intention in case the keys are changed later.

Last Post!

dli

dli

It uses the order in which you define the columns. It’s unfortunately a bit unintuitive for people used to real SQL :slight_smile:

Damn, that’s what I thought :wink:

Something like create primary_key(...) would be great, similar to create index(...).

in Postgres, you do not need null: false on primary key columns

I know, I just use this to clarify the intention in case the keys are changed later.

Where Next?

Popular in Questions Top

RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

Other popular topics Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44139 214
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42533 114
New

We're in Beta

About us Mission Statement