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.

Where Next?

Popular in Questions Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
Werner
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”: 14:57:30.512 [warn] ...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

Other popular topics Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement