How do I avoid Ecto matching incorrectly when comparing binary IDs with == in a Latin database?

浪尽此生 提交于 2020-07-10 03:25:39

问题


I've come across a problem with my Elixir Ecto MySQL database. We've found that the character set is latin, which is probably part of the problem.

I have an object in the database with the id: field set to "3f0c7254-693f-4574-3f3f-256c3f3f5224". If I do a query on the database using this block of Elixir to generate the query:

params
|> Enum.reduce(@schema, fn {k, v}, query ->
  where(query, [b], field(b, ^k) == ^v)
end)

I can pass id: "b30c7254-69d1-4574-a489-256cd9c45224" to the query (as params) and it will find the record with the other id value. Now, note that the difference between the two strings is that the stored value contains several instances of "3f" which is the ASCII value of "?" and that the corresponding "character" in the search string is 0x80 or above. So I think what is happening is that the query is converting the "invalid Latin characters" to an ASCII "?" and then doing a character match.

Why are these things in quotes being treated as if they are representations of characters, instead of being characters themselves?

Is there a way to avoid this problem? Is the only solution to alter the database character set to UTF8? It seems like the database queries should be able to work in any character set.

The project is using some older versions of things and we are looking at upgrades for several reasons, but I am hoping for a solution to this rather quickly as the general form of the problem is causing problems in production.

Elixir 1.7, Ecto 2.1.6, MariaEx 0.8.4

来源:https://stackoverflow.com/questions/61946283/how-do-i-avoid-ecto-matching-incorrectly-when-comparing-binary-ids-with-in-a

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!