【发布时间】:2017-03-06 08:04:24
【问题描述】:
这样的简单查询。
MyModel.where('student_id = :id AND from <= :date AND to >= :date', {:id => student.id, :date => day.to_s(:db)})
返回异常
=> ActiveRecord::JDBCError: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'from <= '2017-02-28 00:00:00.000' AND to >= '2017-02-28 00:00:00.000')'
我不明白为什么。
【问题讨论】:
-
FROM是MySQL的保留关键字 -
@Raptor 射击。这就是我的想法。但是我的表有一个字段
from。那我该怎么办? -
简单:重命名。避免在列名和表名中使用保留字。
-
@Forward 不推荐,因为这会造成混淆
-
糟糕,添加反引号是唯一的解决方案,否则您必须在下一个维护窗口重命名 DB 列以及所有相关代码。
标签: mysql sql ruby ruby-on-rails-3 activerecord