type1fool

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.

Showing Posts 1 to 7

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.

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.

type1fool

type1fool OP

@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.

type1fool

type1fool OP

I’ll also add a link to this episode of Full Stack Radio, which helped me understand multi-tenancy and considerations. It focuses mostly on database design instead of language-specific solutions.

tielur

tielur

@michalmuskala Is there a reason why you’d suggest has_many through vs a many_to_many? I recently went down this route and ended up using a many_to_many relationship. I suppose the biggest difference is being able to have metadata on the join?

michalmuskala

michalmuskala

many_to_many is all about hiding the join table. In this use case, as far as I understand, a precise management of data in the join table is required and direct access to the other table (roles) is primarily for reading - that’s the use case of has_many through:.

In general, though, I personally find many_to_many not that useful and almost always go for has_many through. It gives me more control and with Ecto.Multi is not that much more code to do inserts and similar manually.

OvermindDL1

OvermindDL1

This right here. It’s surprising what you end up adding to a join table over time.

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
jaybe78
Hello, I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter). The diffic...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New

Other Trending Topics Top

manuel-rubio
Because I don’t want to continue using wkthmlpdf or chromium/chrome for only exporting a HTML to PDF, I decided to create a library calle...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews