Valian

Valian

It’s my first post on this forum, so hi everyone! I’m coming from Python / Django background but been heavily looking into Elixir for past 2-3 years. I decided to use Phoenix in my newest project. I have a question I can’t resolve by myself in a clean way and I can’t find an answer.

I’m building a multi-tenant application. Some tables are in public schema, and some are in tenant-specific schemas. I’m using :prefix to work with it, pretty standard stuff. In tests I’m using factories pattern described here

The problem boils down to inserting nested records into multiple schemas. Example:

game = %Myapp.Game{name: "test"}  # Game has @schema_prefix "public"
session = %Myapp.GameSession{game: game}
Repo.insert!(session, prefix: "tenant")

Error:

** (Postgrex.Error) ERROR 42P01 (undefined_table) relation "tenant.game" does not exist

I understand why it happens, prefix option to Repo.insert! has priority over @schema_prefix. I tried to use game = %Myapp.Game{name: "test"} |> Repo.put_meta(prefix: "public") but it changed nothing. The only workaround I found is to insert game separately and then pass game_id to the session. Still, this gets complex quickly since as I said I’m using factory pattern with helper, and it forces me to provide not only build(:type) but also insert(:type) each time.

  def insert!(factory_name, attributes) do
    factory_name |> build(attributes) |> Repo.insert!
  end

So ultimately my question: is there a way to set prefix for unsaved nested record, so it will be honored by Repo.insert!?

Showing Posts 1 to 4

aziz

aziz

Hi and welcome!

Did you try this yet?

game = %Myapp.Game{name: "test"}  # Game has @schema_prefix "public"
session = %Myapp.GameSession{game: game} |> Repo.put_meta(prefix: "tenant")
Repo.insert!(session)

My guess is it won’t work because the docs say that the parent schema’s prefix will override all its childrens’ prefix.

:prefix - The prefix to run the query on (such as the schema path in Postgres or the database in MySQL). This overrides the prefix set in the query and any @schema_prefix set any schemas. Also, the @schema_prefix for the parent record will override all default @schema_prefixs set in any child schemas for associations.

Maybe it’s a limitation in Ecto that should be discussed given your case.

Valian

Valian OP

It works!

So it looks like instead of using Repo.insert!(prefix: prefix) I need to use Repo.insert!() and set prefix in individual nested models.

It’s counter-intuitive - instead of setting a well-known “public” prefix for shared models, and specifying runtime-determined tenant prefix in insert, I have to avoid using insert! prefix entirely, otherwise it will override everything set in individual associations…

I think prefix in insert! shouldn’t change associated prefixes, if set (either through @schema_prefix or Ecto.put_meta). Do you think it’s worth discussing it? If yes, should I do it here or in a github issue?

aziz

aziz

Ha! That’s great! Didn’t expect that really. :laughing:

We should probably discuss it here first. I’m not sure but I think it’s more intuitive that Repo.insert!(x, prefix: prefix) would override the prefix for all structs. After all, if you don’t want that behaviour then you simply don’t pass the option. If it were the other way around and it didn’t override all the prefixes, then I guess we’d have to invent another setting like override_prefixes: true in order to have them overridden in case we actually meant to do that. I don’t know if that’s how @josevalim thinks and if that was his design decision but maybe he can comment shortly to help clarify the reasoning behind this. :slight_smile:

PS.: Could you pls mark my post above as the solution? :smiley:

Valian

Valian OP

I’ve created an issue in ecto github Saving nested records with prefixed multitenancy · Issue #3827 · elixir-ecto/ecto · GitHub.

Jose replied almost immediately, explained why it’s working in the current way and updated docs to make it clear :slight_smile: I truly love that community, amazing!

— All posts loaded —

Where Next? Top

Trending in Questions Top

Hellowman
Hello everyone! I am new to coding with Elixir/Erlang. And i am looking for some utilities for fuzzing testing like AFL++. Does anyone ...
New
yarrichar
I’m very new to Elixir, so sorry if this is a silly question: I am wondering what happens to the mailbox of a process when the machine i...
New
pragdave
How do I tell Hologram not to escape HTML when substituting into templates? {Textile.render(@product.long_description)} escapes the HTM...
New
Fl4m3Ph03n1x
Background When trying to execute mix release on a Windows 11 machine for a Phoenix project I get the following error: * assembling mark...
New
mandar1045
I load tested a plain Phoenix chat app backed by Postgres with 100 million messages in it, everything on default settings, to see what br...
New
rlipscombe
I’d like to create a file with 600 permissions (rw-------), but there’s a race condition if I create it and then change the permissions. ...
New
saleyn
Is there a way to import record definitions in the iex shell for pretty printing (similar to the rr command in the erl shell)? This woul...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
261 13183 112
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
OndrejValenta
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly) AI dream has many faces, but general...
New
kip
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
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

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews