2016-11-07 4 views
0

は、私はテーブルには、次のように説明している:私はあなたがすべてのこれらの3つのフィールドを見ることができるように を(COUNTRY_ID、from_country_id、to_country_id)の外部キーを作成しようとしていますPostgresのDatebaseマルチ外部キー

         Table "public.lead" 
      Column   |    Type    |    Modifiers     
-----------------------------+--------------------------------+----------------------------------------- 
id       | character varying(36)   | not null 
reference_code    | character varying(20)   | not null 
country_id     | character varying(36)   | not null 
language_id     | character varying(36)   | not null 
locale_id     | character varying(36)   | not null 
from_country_id    | character varying(36)   | not null 
to_country_id    | character varying(36)   | not null 
customer_id     | character varying(36)   | not null 
user_id      | character varying(36)   | 
from_date     | date       | not null 
from_date_type    | smallint      | not null default (0)::smallint 
from_street     | character varying(200)   | 
from_postalcode    | character varying(25)   | 
from_city     | character varying(100)   | 
from_country    | character varying(50)   | 
from_apartment_type   | character varying(255)   | not null default '0'::character varying 
from_floor     | smallint      | 
from_rooms     | numeric(3,1)     | 
from_people     | integer      | 
from_squaremeter   | integer      | 
from_elevator    | smallint      | not null 

を関係を持っていますテーブル付き。 しかし、これらの外部キーを作成しようとすると、次のエラーが発生します。

ERROR: insert or update on table "lead" violates foreign key constraint "lead_to_country_id" Detail: Key (to_country_id)=(United Kingdom) is not present in table "country". Details

+0

エラーは、 'insert'または' update'ステートメントによって発生します。あなたの質問とそのすべてのステートメントを[編集]してください。 –

答えて

0

このエラーは、しばしば欠落しているキーに関連しています。

insert文の後に外部キーを作成しようとすると、SQLは主キー(PK)を持つ表のキーを検索します。

例:

table_with_PK

col1(Pk) | col2| coln ... 
id_1  foo bar ... 
id_2  nan ana ... 

table_connected_to_table_with_PK

col1(Fk) | col2 | etc... 
id_1 
id_2 
id_3 (Error because not present in table_with_PK) 

だから、最初にあなたの主キーを持つテーブルを作成し、それを取り込みます。

第2に、データベースに一貫性を持たせるために、外部キーを使用してテーブルを作成し、外部キー(Fk)を作成し、それを移入/更新します。制約の

チェックPostgreSQLのドキュメントは:https://www.postgresql.org/docs/current/static/ddl-constraints.html

0

エラーメッセージがかなりそれを言う:あなたが参照さcountryテーブルに存在しない'United Kingdom'からto_country_id列の値を設定しようとしています。その値をcountryテーブルに挿入して、再試行してください。