与纯sql查询相比,Rails控制台查询返回不同的结果
因此,在我的 Rails 控制台中,我有一个生成 SQL 的活动记录查询。我查看我的 logs/development
,可以看到正在生成的 SQL。
由于某种原因,当我运行活动记录查询时,我没有得到预期的结果。我查看了日志并将 sql 复制/粘贴到连接到数据库的控制台中,然后突然返回了正确的结果。有什么想法为什么会发生这种情况吗?我正在使用 PostgreSQL。
我正在尝试查找过去 5 小时内开业的商店。
编辑减去 10 小时使此查询工作......
ActiveRecord 调用(内部来自 geokit gem):
Store.within(10, :origin =>[30.267153000000000, -97.743060799999970]).where("date > current_timestamp - interval '5 hours'")
生成 SQL:
SELECT "store".*,
(ACOS(least(1,COS(0.5282614750548792)*COS(-1.705938231937002)*COS(RADIANS(store.lat))*COS(RADIANS(store.lng))+
COS(0.5282614750548792)*SIN(-1.705938231937002)*COS(RADIANS(store.lat))*SIN(RADIANS(store.lng))+
SIN(0.5282614750548792)*SIN(RADIANS(store.lat))))*3963.19)
AS distance FROM "store" WHERE ((store.lat>30.122583147146404 AND store.lat<30.41172285285359 AND store.lng>-97.91044799232348 AND store.lng<-97.57567360767642)) AND ((
(ACOS(least(1,COS(0.5282614750548792)*COS(-1.705938231937002)*COS(RADIANS(store.lat))*COS(RADIANS(store.lng))+
COS(0.5282614750548792)*SIN(-1.705938231937002)*COS(RADIANS(store.lat))*SIN(RADIANS(store.lng))+
SIN(0.5282614750548792)*SIN(RADIANS(store.lat))))*3963.19)
<= 10)) AND (date > current_timestamp - interval '5 hours')
So in my Rails console I have an active record query that generates an SQL. I look in my logs/development
and I can see the SQL that is being generated.
For some reason I'm not getting the expected results when I run the active record query. I looked into my logs and copied/pasted the sql into the console that is connected to my database and then suddenly that returns the correct results. Any ideas why this is happening? I'm using PostgreSQL.
I'm trying to find stores that were opened in the past 5 hours.
EDIT subtracting 10 hours makes this query work....
ActiveRecord call (within is from geokit gem):
Store.within(10, :origin =>[30.267153000000000, -97.743060799999970]).where("date > current_timestamp - interval '5 hours'")
SQL generated:
SELECT "store".*,
(ACOS(least(1,COS(0.5282614750548792)*COS(-1.705938231937002)*COS(RADIANS(store.lat))*COS(RADIANS(store.lng))+
COS(0.5282614750548792)*SIN(-1.705938231937002)*COS(RADIANS(store.lat))*SIN(RADIANS(store.lng))+
SIN(0.5282614750548792)*SIN(RADIANS(store.lat))))*3963.19)
AS distance FROM "store" WHERE ((store.lat>30.122583147146404 AND store.lat<30.41172285285359 AND store.lng>-97.91044799232348 AND store.lng<-97.57567360767642)) AND ((
(ACOS(least(1,COS(0.5282614750548792)*COS(-1.705938231937002)*COS(RADIANS(store.lat))*COS(RADIANS(store.lng))+
COS(0.5282614750548792)*SIN(-1.705938231937002)*COS(RADIANS(store.lat))*SIN(RADIANS(store.lng))+
SIN(0.5282614750548792)*SIN(RADIANS(store.lat))))*3963.19)
<= 10)) AND (date > current_timestamp - interval '5 hours')
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
此 WHERE 子句的标准 SQL 语法应为
在标准 SQL 中,date 是保留字;用作列名的保留字需要用双引号引起来。请注意,数字位于单引号内,并且关键字 interval 和 hour 未加引号。对标准 SQL 的支持差异很大,尤其是在您所在的社区。 (引用保留字和日期算术。)
所以。 。 。检查您的目标平台的文档,以确保您正确引用(或不引用)这两件事。
不过,我很难说服自己这可能是导致您问题的原因。
Standard SQL syntax for this WHERE clause should be
In standard SQL, date is a reserved word; reserved words used as column names need to be in double quotes. Note that the number is within single quotes, and that the keywords interval and hour aren't quoted. Support for standard SQL varies wildly, especially in your neighborhood. (Quoting of reserved words and date arithmetic.)
So . . . check the docs for your target platform to make sure you're quoting (or not quoting) these two things correctly.
I'm having a hard time convincing myself that this could the the cause of your problem, though.