org.postgresql.util.PSQLException:错误:在表上插入或更新违反了外键约束

时间:2019-01-23 06:54:23

标签: postgresql spring-boot jpa foreign-keys sql-insert

org.postgresql.util.PSQLException: ERROR: insert or update on table "party_custom_fields" violates foreign key constraint "fk21oqkpi7046skme7jce06fxdu"

下面的错误可能会有所帮助,需要对代码做些什么,我尝试了很少的参考,但没有帮助。

Detail: Key (custom_field_value)=(11) is not present in table "custom_field_value"

以上是我保存时的错误。

Party是将具有自定义字段及其数据的类


import lombok.Data;

import javax.persistence.*;
import java.util.HashMap;
import java.util.Map;

@Entity
@Data
@Table(name = "party")
public class Party {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;
    @Column(name = "last_name")
    private String lastName;
    private String email;
    private String gender;
    @OneToMany(cascade = CascadeType.ALL)
    @JoinTable(name = "party_custom_fields",
            joinColumns = {@JoinColumn(name = "custom_field")},
            inverseJoinColumns = {@JoinColumn(name = "custom_field_value")})
    @MapKeyColumn(name = "custom_field_key")
    private Map<Long, CustomFieldValue> customField = new HashMap<>();
    public Party() {
    }
    public Party(String name) {
        this.name = name;
    }

}

自定义字段值模型

package org.aeq.multitenant.model;

import lombok.Data;

import javax.persistence.*;

@Data
@Entity
@Table(name = "custom_field_value")
public class CustomFieldValue {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String value;

}

自定义字段模型,其中包含表的自定义字段

package org.aeq.multitenant.model;

import lombok.Data;
import org.aeq.multitenant.enums.Tables;

import javax.persistence.*;

@Data
@Entity
@Table(name = "custom_field")
public class CustomField {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;
    private String type;
    private boolean optional;
    @Enumerated(EnumType.STRING)
    @Column(name = "table_name")
    private Tables tableName;
}

要保存的控制器功能

 Map<Long, CustomFieldValue> cfMap = new HashMap<>();

        for (CustomField cf : customFields) {
            if (!partyData.containsKey(cf.getName())) {
                return new ApiResult<>(false, "Please provide " + cf.getName() + " custom field of party");
            } else {
                CustomFieldValue cfv = new CustomFieldValue();
                cfv.setValue(partyData.get(cf.getName()).trim());
                cfv = customFieldValueRepository.save(cfv);
                cfMap.put(cf.getId(), cfv);
            }
        }

        Party party = new Party();
        party.setName(partyData.get("name"));
        party.setEmail(partyData.get("email").trim());
        party.setGender(partyData.get("gender").trim());
        party.setLastName(partyData.get("last_name").trim());
        party.setCustomField(cfMap);

        party = partyRepository.save(party);

请查看我的代码,让我知道我要去哪里

1 个答案:

答案 0 :(得分:0)

如果列具有外键约束,则该列的任何条目都应存在于给定的引用表中。如果没有,则将引发此异常。