Ecto filters for conditions other than equality

Hi there,

I was wondering what approach you would recommend for adding update conditions to an Ecto changeset that do not just check for equality but maybe do a comparison with the < operator or use the SQL keyword LIKE.
What I’m trying to do is allow the update of a database record only if a particular integer field is smaller or equal to a specific value. I tried putting something like %{field: “<n”} or %{field: 0…n} in Ecto.Changeset.filters but obviously Ecto was unable to do anything with those statements. The great advantage of filters is of course that you don’t need to query the database twice.

1 Like

If the value is not dynamic, maybe you could use Postgres check constraints? https://en.wikipedia.org/wiki/Check_constraint

Then you can use Ecto.Changeset.check_constraint to convert those into nice errors. Otherwise, please open up an issue. We can probably do a better job in generalizing filters now that we have dynamic queries.

2 Likes