DEV Community

TIL: Ecto's pin is coercing

Lasse Skindstad Ebert on October 21, 2019

TL;DR This: query |> where([account], account.owner_id == "42") is not the same as this: owner_id = "42" query |> where([a...
Collapse
 
ryanwinchester profile image
Ryan Winchester • Edited

If you aren't using a schema, you can also define the type in the query like:

owner_id = "42"

where(
  query,
  [account],
  account.owner_id == type(^owner_id, :integer)
)
Enter fullscreen mode Exit fullscreen mode
Collapse
 
exadra37 profile image
Paulo Renato

Thanks for sharing, always learning something new every day ;)

In Postgres I don't know, but in MySql comparing a string with an integer works but its very inefficient in terms of query performance.

Collapse
 
lasseebert profile image
Lasse Skindstad Ebert

Thanks for reading 🙂

I think comparing string with integer can't be done in postgres, but the above error is from Ecto, which will complain before it even hits the db.