Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails ActiveRecord Postgres适配器生成无效的SQL WHERE子句:WHERE(";fieldname";)_Ruby On Rails_Postgresql_Activerecord_Ruby On Rails 4_Rails Activerecord - Fatal编程技术网

Ruby on rails ActiveRecord Postgres适配器生成无效的SQL WHERE子句:WHERE(";fieldname";)

Ruby on rails ActiveRecord Postgres适配器生成无效的SQL WHERE子句:WHERE(";fieldname";),ruby-on-rails,postgresql,activerecord,ruby-on-rails-4,rails-activerecord,Ruby On Rails,Postgresql,Activerecord,Ruby On Rails 4,Rails Activerecord,ActiveRecord 4.1.1正在生成一些postgres无法使用的特殊SQL保存: SELECT "projects".* FROM "projects" WHERE ('uid') LIMIT 1 PG::InvalidTextRepresentation: ERROR: invalid input syntax for type boolean: "uid" 我很确定这里提到的“boolean”实际上是WHERE()语法,它需要一个布尔表达式作为参数 uid被定义为我的型号上

ActiveRecord 4.1.1正在生成一些postgres无法使用的特殊SQL保存:

SELECT  "projects".* FROM "projects"  WHERE ('uid') LIMIT 1
PG::InvalidTextRepresentation: ERROR:  invalid input syntax for type boolean: "uid"
我很确定这里提到的“boolean”实际上是
WHERE()
语法,它需要一个布尔表达式作为参数

uid
被定义为我的型号上的主键(见下文)。。。但为什么它在执行select\u all(请参阅stacktrace)时只是说
WHERE('uid')
?(为什么要在保存时选择所有?)

轨道控制台 Gemfile.lock
你的调情毫无意义。此处的这一部分导致了错误的SQL:

self.class.find_by(:uid, self.id)
这会产生无效的SQL,因为您要它这样做。有一个灵活的接口,所以它必须进行一些解析,以查看您如何调用它。当你说:

find_by(a, b)
find_by(:uid, self.id)
参数解析可能假设您正在尝试使用以下表单:

find_by('some_sql_snippet', placeholder_valid)
如文档中的本例所示:

find_by("published_at < ?", 2.weeks.ago)
您将得到
:uid.to_s
self.id
将被忽略,因为该字符串中没有占位符。这可以解释为什么您在SQL中看到
where('uid')

修正你的混音,使其有意义:

self.class.find_by(:uid => self.id)
或者更好,切换到
存在?
以避免构建整个模型实例,只是为了查看数据库中是否有行:

self.class.where(:uid => self.id).exists?
self.class.exists?(:uid => self.id)

你对错误的猜测是正确的;注意:
选择1,其中('uid')生成
错误:布尔类型的输入语法无效:“uid”
。啊哈!一些旧的Rails2代码没有跳转到4(mysql也没有阻塞)!谢谢你的接球!
find_by("published_at < ?", 2.weeks.ago)
find_by(:uid, self.id)
self.class.find_by(:uid => self.id)
self.class.where(:uid => self.id).exists?
self.class.exists?(:uid => self.id)