title: org.postgresql.util.PSQLException: ERROR: insert or update on table violates foreign key constraint
body: <pre><code>org.postgresql.util.PSQLException: ERROR: insert or update on table "party_custom_fields" violates foreign key constraint "fk21oqkpi7046skme7jce06fxdu"
  </code></pre>
  
  <p>Below error could help, what need to be done on the code, I have tried few reference, but not helpful. </p>
  
  <p><code>Detail: Key (custom_field_value)=(11) is not present in table "custom_field_value"</code></p>
  
  <p>Above is my error while saving.</p>
  
  <p>Party is the class which will have custom fields and it's data</p>
  
  <pre><code>
  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&lt;Long, CustomFieldValue&gt; customField = new HashMap&lt;&gt;();
      public Party() {
      }
      public Party(String name) {
          this.name = name;
      }
  
  }
  </code></pre>
  
  <p>Custom fields value model</p>
  
  <pre><code>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;
  
  }
  
  </code></pre>
  
  <p>Custom fields model which hold what are the custom fields for the tables</p>
  
  <pre><code>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;
  }
  </code></pre>
  
  <p>Controller function to save</p>
  
  <pre><code> Map&lt;Long, CustomFieldValue&gt; cfMap = new HashMap&lt;&gt;();
  
          for (CustomField cf : customFields) {
              if (!partyData.containsKey(cf.getName())) {
                  return new ApiResult&lt;&gt;(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);
  </code></pre>
  
  <p>please review my code and let me where I am going wrong</p>