使用关联(belongs_to)模型获取以下错误:
PG::ForeignKeyViolation: ERROR: insert or update on table "employees" violates foreign key constraint "fk_rails_0d62068e18" DETAIL: Key (contract_type_id)=(2) is not present in table "contract_types". : INSERT INTO "employees" ("title_id", "surname", "othernames", "department_id", "job_title_id", "gender_id", "date_of_birth", "contract_type_id", "employee_status_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11) RETURNING "id"
在这种情况下,contract_type_id
是排除模型(ContractType)
据报道该错误来自这一行:
@connection.exec_prepared(stmt_key, type_casted_binds.map { |_, val| val })
我正在使用带有config.use_schemas = true
选项的postgres。
答案 0 :(得分:2)
如果我正确理解您的模型关系,this may help readers understand the problem。简短:公寓不支持非排除模型与排除模型之间的外键约束。