Owens

Owens

Unique Index/Constraint allow duplicates ONLY when ALL references are null

Hello all,

I’m creating a chat app that automatically creates chats between different groups of people as well as individuals. I want to make sure it doesn’t create any duplicates for the chats between these groups, but also allows for individual chats (private) where all the references are nil.

create unique_index(:chats,
  [:parent_organization_id, :organization_id, :cohort_id, :team_id])

My understanding is that if any reference above is nil, postgres will allow duplicates.

To fix this, @LostKobrakai recommends here to create indexes with coalesce’d values like so:

execute( "create unique index INDEX_NAME on TABLE
  (
    coalesce(parent_organization_id, ''),
    coalesce(organization_id, ''),
    coalesce(cohort_id, ''),
    coalesce(team_id, '')
  );
")

But the problem is I need to ALLOW duplicates when all the references are nil (which they will be for individual chats between users).

In other words,

I need to PREVENT duplicates for these

parent_organization_id: 1, organization_id: nil, cohort_id: nil, team_id: nil
parent_organization_id: 1, organization_id: 1,   cohort_id: nil, team_id: nil
parent_organization_id: 1, organization_id: 1,   cohort_id: 1,   team_id: nil

parent_organization_id: nil, organization_id: 1,   cohort_id: nil, team_id: nil
parent_organization_id: nil, organization_id: 1,   cohort_id: 1,   team_id: nil

and ALLOW duplicates for these

parent_organization_id: nil, organization_id: nil, cohort_id: nil, team_id: nil

Appreciate any thoughts. Thank you.

Marked As Solved

Owens

Owens

It worked after changing

coalesce(parent_organization_id, '')

to

coalesce(parent_organization_id, -1)

Full code:

def change do
    drop unique_index(:chats, [:parent_organization_id, :organization_id, :cohort_id, :team_id])

    execute( "create unique index chats_parent_organization_id_organization_id_cohort_id_team_id_index on chats
        (
            coalesce(parent_organization_id, -1),
            coalesce(organization_id, -1),
            coalesce(cohort_id, -1),
            coalesce(team_id, -1)
        ) where type != 'direct'
    ")
end

Also Liked

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

@Owens One certainly easy option here is an additional direct or individual column that you set to true, and then you can just WHERE individual == false on your unique index. This does mean you have to set that value, but OTOH it also makes it much more explicit, whereas the version that relies on N columns being nil feels very implicit.

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe
xecute( "create unique index INDEX_NAME on TABLE
  (
    coalesce(parent_organization_id, ''),
    coalesce(organization_id, ''),
    coalesce(cohort_id, ''),
    coalesce(team_id, '')
  ) where individual == false
")

This marks what is called a “partial index” where the index only applies to the rows where the condition is true.

Where Next?

Popular in Questions Top

nobody
How to bind a phoenix app to a specific ip address? could not find anything about that, nowhere, unfortunately, but for me this is quite...
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
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
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
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
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
earth10
Hi, I’m just starting to build a side-project with Elixir and Phoenix and doing some basic test with Elixir alone. What strikes me is th...
New

Other popular topics Top

skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
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
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52673 488
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
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
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...
3271 127089 1222
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54120 245
New

We're in Beta

About us Mission Statement