Reputation: 53
I'm still new to postgreSQL. I have two tables I created that I want to go back and add Primary and Foreign key constraints for and no matter what I do I can't seem to add a foreign key. Here's what I have:
Two Tables:
test=# \d statename
Table "public.statename"
Column | Type | Modifiers
--------+---------------+-----------
code | character(2) | not null
name | character(30) |
Indexes:
"statename_pkey" PRIMARY KEY, btree (code)
test=# \d customer
Table "public.customer"
Column | Type | Modifiers
-------------+---------------+-----------
customer_id | integer |
name | character(30) |
telephone | character(20) |
city | character(25) |
street | character(40) |
state | character(2) |
zipcode | character(10) |
country | character(20) |
Here's the command I'm running:
test=# ALTER TABLE customer ADD CONSTRAINT
state FOREIGN KEY (code) REFERENCES
statename (code) >MATCH FULL;
Here's the error I'm getting:
ERROR: column "code" referenced in foreign key constraint does not exist
I'm looking at the column! I know it exists! Please help a brother out!
Upvotes: 5
Views: 23519
Reputation: 23318
Code does not exist in your customer table. It's state.
ALTER TABLE customer
ADD CONSTRAINT state FOREIGN KEY (state)
REFERENCES statename (code) >MATCH FULL;
Upvotes: 3