[英]operator does not exist: timestamp without time zone ~~* unknown ruby code
I am try to query users in the database when I do a search. 搜索时,我尝试查询数据库中的用户。 However i am getting a specific error which is 'operator does not exist: timestamp without time zone ~~* unknown'
但是我遇到一个特定的错误,即“操作员不存在:没有时区的时间戳~~ *未知”
In my database, the record of data_of_birth is saving in the format below. 在我的数据库中,data_of_birth的记录以以下格式保存。 Now sure what I am missing.
现在确定我所缺少的。 Code below
下面的代码
field == "dob"
selected_date = Date.strptime(query, '%Y-%m-%d')
items = Patient.joins(role: :user).where('users.date_of_birth ILIKE :search', search: "%#{selected_date}%")
Here how date_of birth is saving in the DB 这里date_of birth是如何保存在数据库中的
date_of_birth: "1997-03-29 00:00:00"
Timestamps are stored as numbers in the database, not as strings, so using string comparisons (eg ILIKE
) won't work. 时间戳记以数字形式存储在数据库中,而不是以字符串形式存储,因此使用字符串比较(例如
ILIKE
)将不起作用。
It sounds like you're trying to match timestamps to dates. 听起来您正在尝试将时间戳与日期进行匹配。 You can simply cast the timestamp to a date for that.
您可以简单地将时间戳转换为该日期。 Something like:
就像是:
WHERE users.date_of_birth::date = '2019-06-14'
That would seem to translate to the following, based on your code. 根据您的代码,这似乎可以转换为以下内容。
field == "dob"
selected_date = Date.strptime(query, '%Y-%m-%d')
items = Patient.joins(role: :user).where('users.date_of_birth::date = :search', search: "%#{selected_date}%")
I'm unfamiliar with the libraries you're using, but it looks like the "::date" part might run into the placeholder functionality. 我不熟悉您使用的库,但看起来“ :: date”部分可能会遇到占位符功能。 You may need to tinker with that in order for it to work.
您可能需要对此进行修补才能起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.