Rails4 FIND_IN_SET with PG not working

醉酒当歌 提交于 2020-01-06 01:29:49

问题


Query

select * 
from inboxes 
where find_in_set ('6',inboxes.to);

Above query i am using for accessing record from my table 'inboxes' having one of the column 'to' values like (2,4,6,7) string separated by commas. I am using find_in_set function in pg but it shows me error with below hint.

Hint: No function matches the given name and argument types. You might need to add explicit type casts.


回答1:


You can simulate MySQL's find_in_set() function using an array in Postgres.

select * 
from inboxes 
where '6' = any (string_to_array("to",','));

Note that to is a reserved keyword and thus needs to be quoted "to". In general you should not reserved keywords as column names (or any other identifier)


But you should really think about fixing your data model. Your model is violating the first normal form and will give you many more headaches in the long run. One of the biggest problems is that you can't define any foreign key constraints on the values stored in that string value, or can you enforce any other constraints on the data.



来源:https://stackoverflow.com/questions/32391486/rails4-find-in-set-with-pg-not-working

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