Return records that don't have specific key - Rails4 HStore Postgresql query

为君一笑 提交于 2019-12-10 11:15:59

问题


I have a model Task

t.string   "name"
t.hstore   "actions"

Example:

#<Task id: 1, name: "first", actions: {"today"=>"9"}, 
#<Task id: 2, name: "second", actions: {"yesterday"=>"1"}, 
#<Task id: 3, name: "third", actions: nil,
#<Task id: 4, name: "four", actions: {"today"=>"11"},

I need to find all records where actions: nil, :today<10 and key :today not present. It is 1,2,3 task.

Task.where("actions -> 'today' < '10'") - it return only first task.
Task.where("actions -> 'today' < '10' OR actions IS NULL")  - it return 1 and 3 records.

How i can find all records which do not have the key :today in action?


回答1:


From here, according to your question:

Task.where("actions IS NULL OR EXIST(actions, 'today') = FALSE")

and according to your comments:

Task.where("actions -> 'today' < '10' OR actions IS NULL OR EXIST(actions, 'today') = FALSE")


来源:https://stackoverflow.com/questions/17645329/return-records-that-dont-have-specific-key-rails4-hstore-postgresql-query

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