njwest

njwest

Polymorphic/Many-to-Many table relationship with dynamic IDs

Hiya, we’re designing PostgreSQL schemas for an app that has Users, and the user has Things of different types as well as Affinities for those Things, through a dynamic many-to-many table:

users_things_and_affinities table:

user_id
thing_type
thing_id
affinity_id

The idea is to have various many-to-many relationships between users and different tables within a certain category through this table (as an efficiency for a different feature of the app).

Things can refer to various different tables (e.g. tools, locations, buildings, etc)

Example record:

user_id = 1, thing_type = buildings, thing_id = 5, affinity_id = 4

Is there an efficient way to make dynamic foreign keys such as this using Ecto, or are we better off with a table with nullable Thing columns such as:

user_id
affinity_id
building_id
tool_id
location_id
person_id
activity_id

where a possible record could be:

user_id = 1
affinity_id = 4
building_id = null
tool_id = 10
book_id = null
person_id = null
activity_id = null

We’re opting to go with a dynamic many-to-many table such as this one rather than creating a bunch of different user_x_and_affinity many-to-many tables so we don’t have to do lots of joins/subqueries when calculating this user data altogether, but I am very open to other viewpoints :stuck_out_tongue:

Most Liked

al2o3cr

al2o3cr

You could use an additional intermediate table as a “handle” to the many kinds of things:

user_things_and_affinities:
user_id
thing_id
affinity_id

things:
type
building_id
tool_id
location_id
person_id
activity_id
etc

This way, for M users and N things there are only N rows with mostly NULL instead of O(M*N).

You could also copy some data to the things table from each kind of record - for instance, a “display name” so that UI code doesn’t need to load the associated record just for a name.

Where Next?

Popular in Questions Top

Tee
can someone please explain to me how Enum.reduce works with maps
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
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
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
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
gonzofish
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
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
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

Other popular topics Top

JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
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
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
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
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
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
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

We're in Beta

About us Mission Statement