Multi tenancy with foreign keys problem with Repo.get_by

Hello guys,

I’m trying to follow this tutorial:

defmodule MyApp.Repo do
  use Ecto.Repo, otp_app: :my_app

  require Ecto.Query

  def prepare_query(_operation, query, opts) do
    cond do
      opts[:skip_org_id] || opts[:schema_migration] ->
        {query, opts}

      org_id = opts[:org_id] ->
        {Ecto.Query.where(query, org_id: ^org_id), opts}

      true ->
        raise "expected org_id or skip_org_id to be set"

In the login method I need to pass “skip_org_id”, so i did:

  def get_user_by_email_and_password(email, password)
      when is_binary(email) and is_binary(password) do
    user = Repo.get_by(User, email: email, skip_org_id: true)
    if User.valid_password?(user, password), do: user

But it’s not working.

Repo.all(query, skip_org_id: true). works.
Repo.get_by(…, …, skip_ord_id: true). Does not work

Any help?


Found the solution:

Need to pass “email: email” liki this [email: email] at the Repo.get_by

user = Repo.get_by(User, [email: email], skip_org_id: true)