# Multitenancy shared vs seperate schema

**URL:** <https://forum.elixirforum.com/t/multitenancy-shared-vs-seperate-schema/64346>\
**Category:** Discussions\
**Tags:** ecto, phoenix, triplex\
**Created:** [June 19, 2024, 12:46pm UTC](https://forum.elixirforum.com/t/multitenancy-shared-vs-seperate-schema/64346 "2024-06-19T12:46:30Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![timothy](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/timothy/32/21208_2.png) [@timothy](https://forum.elixirforum.com/u/timothy)\
**Post date:** [June 19, 2024, 1:31pm UTC](https://forum.elixirforum.com/t/multitenancy-shared-vs-seperate-schema/64346/2 "2024-06-19T13:31:07Z")

</div>

I never used Triplex, so I can’t say anything on that, but Ecto has a few guides on handling Multi tenancy:

- [Multi tenancy with query prefixes — Ecto v3.14.0](https://hexdocs.pm/ecto/multi-tenancy-with-query-prefixes.html)
- [Multi tenancy with foreign keys — Ecto v3.14.0](https://hexdocs.pm/ecto/multi-tenancy-with-foreign-keys.html)

The company I work at uses FK on all schemas similar to the second guide of Ecto (shared schema). However we never implemented the `prepare_query/3` to add the tenant id to all queries of a repo, since we didn’t like the “magic” of it. Instead we just pass the tenant struct to each function that controls anything related to a tenant (e.g. `list_users(%Tenant{id: tenant_id}), do: %User{} |> where([u], u.tenant_id == ^tenant_id) |> Repo.all()`). It is a bit cumbersome but does provide flexibility and is a bit more explicit on what’s happening. It just comes down to personal taste I guess.

---

_[View the full topic](https://forum.elixirforum.com/t/multitenancy-shared-vs-seperate-schema/64346)._
