Rails的5 PostgreSQL的的has_many,我這裏有3種型號通過我的應用程序關聯
class User < ApplicationRecord
has_many :specialties, class_name: "Specialty",
foreign_key:"teacher_id",
dependent: :destroy
has_many :subjects, through: :specialties, source: :subject
class Specialty < ApplicationRecord
belongs_to :teacher, class_name: "User"
belongs_to :subject, class_name: "Subject"
class Subject < ApplicationRecord
has_many :teachers, through: :specialties, source: :teacher
has_many :specialties, class_name: "Specialty",
foreign_key:"subject_id",
dependent: :destroy
在我的rails控制檯,以前當我使用的SQLite我有 user = User.first
沒有問題,那麼user.subjects
將返回我的所有用戶科目。 但是經過我已經改變到PostgreSQL,它返回我
ActiveRecord::StatementInvalid: PG::UndefinedFunction: ERROR: operator does not exist: integer = character varying
LINE 1: ...ects" INNER JOIN "specialties" ON "subjects"."id" = "special...
^
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
: SELECT "subjects".* FROM "subjects" INNER JOIN "specialties" ON "subjects"."id" = "specialties"."subject_id" WHERE "specialties"."teacher_id" = $1
有什麼方法可以讓我做回了user.subjects? 我注意到,在遵循和遵循rails教程模型時沒有發生這個問題。 (Twitter的模型)
謝謝
編輯,特色菜是不是一個整數列,我要改變它,回來非常感謝!
'「特色菜」,「subject_id」「 - 這是一個整數列? – dp7
哦,它不是! ,我下班後要把它放好。謝謝!! – Holmes
't.string:subject_id' - 它是** String **類型,外鍵的類型應與您的主鍵相同,否則在比較它們時會出現類型轉換錯誤 – dp7