jmurphyweb
Unique constraint with null values
Hi all,
I’ve added a unique constraint across all columns of my project_roles table, which successfully prevents a row being added with all 4 columns filled. However, it’s not playing nicely when one of my values is null.
create table(:project_roles, primary_key: false) do
add :user_id, :integer, null: false
add :role_id, :integer, null: false
add :project_id, :integer, null: false
add :department_id, :integer
end
create unique_index(
:project_roles,
[:user_id, :project_id, :department_id, :role_id],
name: :unique_user_department_role
)
Here’s the constraint:

The constraint successfully prevents the following map being added twice:
%{
user_id: 1,
role_id: 1,
project_id: 1,
department_id: 1,
}
But it allows this maps to be added twice:
%{
user_id: 1,
role_id: 1,
project_id: 1,
department_id: nil,
}
Ideally we would have it fail for multiple entries of that second example.
Marked As Solved
LostKobrakai
You can create indexes with coalesce’d values like so:
execute( "create unique index INDEX_NAME on TABLE (user_id, role_id, project_id, coalesce(department_id, ''));" )
8
Also Liked
BrightEyesDavid
Popular in Questions
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
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
I have followed this StackOverflow post to install the specific version of Erlang.
And When I am running mix ecto.setup then getting fol...
New
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
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible.
total = 10
while total != 0
...
New
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
Hello everybody,
usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
Sometimes I want to check if the input into a function is not a blank string.
My first approach:
defmodule Example do
def do_stuff(s...
New
I have a super simple question about elixir - how would I take a file like this
foo bar baz
and output a new file that enumerates th...
New
Other popular topics
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
New
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
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
Hi! May someone helps me, please!
I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors:
[WARN] - (starship::utils): Executing command ...
New
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
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
New
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
Hi all,
I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage.
I'm trying to use Postg...
New
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New







