我有工作正常使用SQLite但在切換到postgre我有這個查詢的問題一個Rails應用程序:PostgreSQL的破軌查詢功能
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或串它仍然產生上述錯誤。模型設置如下:收視率belongs_to has_many:收視率的用戶。有任何想法嗎?提前致謝。
版本:3.1.1的Rails和Ruby 1.9.2,1.5.1設計
「ratings」表上的'user_id'字段/外鍵肯定是一個整數? – pjumble
不,這不是我改變它,它的工作謝謝你。爲什麼會在sqlite上工作,而不是在Postgre上? – Steve
SQLite有一個相當寬鬆的類型系統,PostgreSQL沒有。您應該在同一個堆棧上開發和部署。 –