-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Multi-tenancy and preloads not working as expected #4407
Comments
This guide explains the order: https://hexdocs.pm/ecto/multi-tenancy-with-query-prefixes.html I believe that, if Oban.Job explicitly lists its prefix as |
I apologize, I'm a bit confused. Reading the section on from/join prefixes, it says you can define a Essentially, how would you fulfill the requirement whereby an invoice in a tenant schema needs to have its oban job fetched from the public schema? The end result I'm trying to achieve is a Repo operation that returns an |
Sure, that could be a feature. However, I still think the best solution in your case is to explicitly declare that Oban Jobs belong to the public prefix via |
Well, it's a third-party library that we have no control over at that level. In any case, I hope this issue can be kept open and the feature I proposed considered for a future release. Thank you. |
Ecto is also a third party library that also does not expose the desired control. If you can ask a feature for Ecto, you can also ask one for Oban and that would arguably be better. :) So I would like to see that attempted first before we add a feature to Ecto that I believe to be a suboptimal solution to the problem presented. |
I understand. I've actually already discussed my use case with Oban's creator. Oban supports multi-tenancy, i.e. one can create an There are also cases where the association table's prefix may also be dynamic. For example, it can be another tenant table. Suppose it's a chat application where users from one organization can join the channels of another organization, like Slack. In such a scenario, the schema name for the association table would not be known in advance and could not be hardcoded into a |
I think there is a good argument for this, but I have a hard time seeing this even be done ergonomically with a table to associate the prefixes and then join them without resorting to raw SQL. But maybe there is something I’m missing since I’m not very well-versed in these scenarios |
I think we should take things one step at a time before the discussion goes in too many directions at once. Let's go back to Jose's question. He asked if you could submit a request to Oban to let you configure The second thing is, you would never have to do that complicated infrastructure setup, even with the way Ecto is today. You can always define a custom preload query instead of using Ecto's default and set the prefix that way. |
Elixir version
1.16.0
Database and Version
14.2
Ecto Versions
3.10.3
Database Adapter and Versions (postgrex, myxql, etc)
Postgrex 0.17.0
Current behavior
This is related to #3387, but pertains to preloading associations where the associated record is on another schema. My use case is a multi-tenant app where each tenant has its own schema. Some tenant records have foreign keys that point to records in the public schema. The relevant tenant migration line looks like this:
The migration itself works fine, i.e. Ecto creates the foreign key association correctly. The problem rears its head when preloading records. For example:
Ecto schema:
This results in:
Expected behavior
Ecto should be"smart" enough to figure out that it should look at the
public.oban_jobs
table to preload the associatedOban.Job
record.The most user-friendly method would be adding a
prefix
option to schema association functions (e.g.belongs_to
) that mirrors theprefix
option inEcto.migration.references/2
.The text was updated successfully, but these errors were encountered: