type1fool

type1fool

Ecto - Array of References?

Hey Elixir community!

I am trying to conceive an application with many users and many organizations. Each user may be a member of multiple orgs, and each org may have multiple users. I’ve been reading up on authorization and relationships in the DB and contexts. @chrismccord’s article was very helpful in illustrating a couple of approaches to authorization.

This might be too simplistic, but I imagined a column (users.roles) with an array of maps similar to this:

[{org_id: 1234, role_title: :owner}, {org_id: 101, role_title: :contributor}]

where org_id would reference an orgs table, and role_title would reference a roles table.

Is it possible to set relations on an {:array, :map} column? I feel like the answer is no, but this is the first solution that comes to mind. I’m having trouble following the official schema documentation, so I really appreciate any guidance.

Thank you.

Most Liked

michalmuskala

michalmuskala

I would advise against keeping references in array or json columns - the reason is that it makes it very easy to get stale data and there’s no way of enforcing foreign key constraints on a data in a “compound” column. I’d suggest using a join table and has_many through.

l00ker

l00ker

You might want to start looking at multi-tenancy using Triplex. You could keep the users table in it’s own schema like public.users and store the tenants they belong to there etc.

Try a search for Triplex and should find a couple of threads here on the forum with more info on the subject.

type1fool

type1fool

@l00ker @michalmuskala Thank you both for the suggestions. I’ll start reading up on has_many through first, and see if I can keep my dependencies minimal.

Where Next?

Popular in Questions Top

New
Tee
can someone please explain to me how Enum.reduce works with maps
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
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
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
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
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
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
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 records...
New

Other popular topics Top

Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
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
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
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
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
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
marick
I had some trouble figuring out how to make many-to-many associations work. Once I got it working, I wrote a blog post. Because I’m a nov...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New

We're in Beta

About us Mission Statement