Search code examples
elixirecto

Ecto query where testA and (testB or testC)


I have a schema that looks like this;

schema "things" do
  field(:title, :string)
  field(:temperature, :integer)
  field(:weight, :integer)
end

I'd like to write an Ecto query that is equivalent to the following SQL, but I can't seem to get it into a valid form.

select * from things where temperature <= 200 and (weight is null or weight > 2);

How can I do this in Ecto?


Solution

  • It's almost the same syntax as SQL:

    from(t in Thing, where: t.temperature <= 200 and (is_nil(t.weight) or t.weight > 2))