fireproofsocks

fireproofsocks

Composite Primary Keys (or multiple unique keys) on Ecto Schema

I’m wondering if I’ve wandered into trouble with my Ecto schemas. I have one that describes files in S3. For convenience (e.g. for REST endpoints), it’s nice to have a singular primary key. But it’s also sane to have a unique constraint on the combination of bucket + object_key. There are a couple ways of describing this for PostGres in the migration. My question is how to describe this in the Ecto schema?

One way is this:

  @primary_key {:id, :binary_id, autogenerate: true}
  schema "files" do
    field(:bucket, :string, primary_key: true)
    field(:object_key, :string, primary_key: true)
    # etc.
    timestamps()
  end

Is that correct? Or is it incorrect because it infers that the composite key is actually id + bucket + object_key?

or would this be more correct:

  @primary_key {:id, :binary_id, autogenerate: true}
  schema "files" do
    field(:bucket, :string)
    field(:object_key, :string)
    # etc.
    timestamps()
  end

How to describe the critical importance of the bucket + object_key constraint?

Thanks in advance for clarity on this!

Marked As Solved

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

If you have a synethetic :id column already, then there is nothing at the schema level that you need to do in Ecto. The database will enforce it for you as long as you set the unique index on the column pair.

It may be useful to add a unique_constraint call to any changesets you build with this schema though to provide a nice error message should the constraint be violated.

Where Next?

Popular in Questions Top

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
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
LegitStack
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it. I’m very interested in Elixir,...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
Kagamiiiii
Student & New to elixir. Nice language. I want to convert a english character, e.g. “a”, which is stored in a variable, to it’s asci...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
makeitrein
Hey all, just started picking up Elixir last week and am writing a scraper as a learning project. Baby step #1 is extracting the number ...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3268 119930 1237
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30048 115
New
yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

We're in Beta

About us Mission Statement