Unsafe_validate_unique with query argument

Hi everyone,

I am trying to use unsafe_validate_unique by passing it a :query arg in the following changeset :

  def changeset(feature_flag_grant, attrs \\ %{}) do
    feature_flag_grant
    |> cast(attrs, [
      :flag_id,
      :entity_id,
      :site_id,
      :enabled
    ])
    |> unique_constraint([:flag_id, :entity_id, :site_id])
    |> unsafe_validate_unique([:entity_id], MyApp.Repo,
      query: from(ffg in __MODULE__, where: is_nil(ffg.site_id))
    )
    |> unsafe_validate_unique([:site_id], MyApp.Repo,
      query: from(ffg in __MODULE__, where: is_nil(ffg.entity_id))
    )
  end

But it does not seem to understand what I am trying to do since I get the following error :

(CompileError) undefined function ffg/0

What am I doing wrong here?

NB: I use ecto_sql 3.7.1

Did you import Ecto.Query?

2 Likes

God I’m so dumb… Sorry about that! That was indeed the problem.

Thank you very much for your help @LostKobrakai !