type1fool
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.
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ai
- #ecto-query
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #metaprogramming
- #hex










Most Liked- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
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
You might want to start looking at multi-tenancy using Triplex. You could keep the users table in it’s own schema like
public.usersand 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
@l00ker @michalmuskala Thank you both for the suggestions. I’ll start reading up on
has_many throughfirst, and see if I can keep my dependencies minimal.Last Post!
OvermindDL1
This right here. It’s surprising what you end up adding to a join table over time.