Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

Ecto multi-tenancy and preloads

27 views
Skip to first unread message

Andrew Timberlake

unread,
Aug 27, 2024, 3:33:43 AM8/27/24
to elixir-ecto
The guide for multi-tenancy with foreign keys [https://hexdocs.pm/ecto/multi-tenancy-with-foreign-keys.html] works well for most queries but can fail when including a preload to tables that don’t include the tenant field.

It would be great if preload queries could include a preload option in the query, similar to migrations, so the following is possible:
CleanShot 2024-08-27 at 09.31.10@2x.png

José Valim

unread,
Aug 27, 2024, 4:31:18 AM8/27/24
to elixi...@googlegroups.com
Please do send a pull request although I wonder if then we should rather introduce a query_type option that can be either :preload or :schema_migrations.

--
You received this message because you are subscribed to the Google Groups "elixir-ecto" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-ecto...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-ecto/04e771ac-26e4-473a-9dd4-66dacfd25adbn%40googlegroups.com.

Andrew Timberlake

unread,
Aug 27, 2024, 4:37:38 AM8/27/24
to elixir-ecto
I would love to. Can you give me guidance as to where the option would be set?
https://github.com/elixir-ecto/ecto/blob/840eedf8357dcf2ebd6c43e716004a5aca483306/lib/ecto/repo/preloader.ex#L324 looks like where the query is done, and that tuplet contains the query opts, but it is set before entering fetch_query
Am I on the right track?

Perhaps instead of `query_type`, it could be `ecto_query: :preload | :schema_migration`
That way you know it’s an ecto generated query and could just do
`if opts[:ecto_query]`
or
`if opts[:ecto_query] == :schema_migration`

—Andrew

On August 27, 2024, jose....@dashbit.co wrote:
Please do send a pull request although I wonder if then we should rather introduce a query_type option that can be either :preload or :schema_migrations.

On Tue, Aug 27, 2024 at 03:33 Andrew Timberlake <and...@andrewtimberlake.com> wrote:
The guide for multi-tenancy with foreign keys [https://hexdocs.pm/ecto/multi-tenancy-with-foreign-keys.html] works well for most queries but can fail when including a preload to tables that don’t include the tenant field.

It would be great if preload queries could include a preload option in the query, similar to migrations, so the following is possible:
<CleanShot 2024-08-27 at 09.3...@2x.png>

--
You received this message because you are subscribed to the Google Groups "elixir-ecto" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-ecto...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-ecto/04e771ac-26e4-473a-9dd4-66dacfd25adbn%40googlegroups.com.
-- 


 You received this message because you are subscribed to a topic in the Google Groups "elixir-ecto" group.
 To unsubscribe from this topic, visit https://groups.google.com/d/topic/elixir-ecto/Y_S6u0WWecc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to elixir-ecto...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-ecto/CAGnRm4JV4KFfTcEc7W5PfSm8FcK7jtAxj2NKYOj%2Bkka0B1fUpA%40mail.gmail.com.

José Valim

unread,
Aug 27, 2024, 4:55:35 AM8/27/24
to elixi...@googlegroups.com
Plus one ecto_query.

And I think you are right about setting it before you call fetch_query, but a unit test would be the best to confirm it. :)

Reply all
Reply to author
Forward
0 new messages