我有一个在 sqlite 上运行良好的 rails 应用程序,但是在切换到 postgre 时,我遇到了这个查询的问题:
User.find(1).ratings
仅查询作品,例如
User.find(1)
生产
SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
但是,如果我像这样附加评级:
User.find(1).ratings
生产
User Load (1.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
Rating Load (0.9ms) SELECT "ratings".* FROM "ratings" WHERE "ratings"."user_id" = 1
PG::Error: ERROR: operator does not exist: character varying = integer
LINE 1: ...CT "ratings".* FROM "ratings" WHERE "ratings"."user_id" = 1
^
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
: SELECT "ratings".* FROM "ratings" WHERE "ratings"."user_id" = 1
ActiveRecord::StatementInvalid: PG::Error: ERROR: operator does not exist: character varying = integer
LINE 1: ...CT "ratings".* FROM "ratings" WHERE "ratings"."user_id" = 1
^
无论是将 :id 作为 int 还是 string 传递,它仍然会产生上述错误。模型设置为 :ratings belongs_to User which has_many :ratings。有任何想法吗?提前致谢。
版本:Rails 3.1.1、Ruby 1.9.2、设计 1.5.1