获取关联(属于)模型的以下错误:

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。

最佳答案

如果我正确理解你的模型关系,this may help readers understand the problem。简而言之:公寓不支持非排除模型到排除模型之间的外键约束。

关于ruby-on-rails - Apartment gem的外键问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29478987/

10-09 13:07