PostgreSQL Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesPostgreSQL Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 2nd, 2003, 11:14 PM
jjoske jjoske is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2001
Posts: 286 jjoske User rank is Lance Corporal (50 - 100 Reputation Level)jjoske User rank is Lance Corporal (50 - 100 Reputation Level)jjoske User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 3 Days 5 h 24 m 46 sec
Reputation Power: 7
unable to create a foreign key

I'm still trying to learn the ins and outs of posgresql. I've got the following table structure:

ingredients
ing_id interger primary key
name
basic_unit

recipes extends ingredients with the following extra columns
recipe_text
amount_made
made_unit

and a table
recipe_ingredients

recipe_id
ing_id
amount_required
weight
id

I'm trying to create a foreign key on recipe_ingredients
recipe_id
on recipes ing_id

using the following statement:
LTER TABLE "recipe_ingredients" ADD FOREIGN KEY ("recipe_id") REFERENCES "recipes"("ing_id") ON UPDATE CASCADE ON DELETE CASCADE

However when ever I do this I get the following error:

ERROR: $3 referential integrity violation - key referenced from recipe_ingredients not found in recipes

Is there any way I can do this, or will I need to look at triggers?

Reply With Quote
  #2  
Old October 3rd, 2003, 09:01 AM
rod k rod k is offline
Apprentice Deity
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Jul 1999
Location: Niagara Falls (On the wrong side of the gorge)
Posts: 3,237 rod k User rank is Private First Class (20 - 50 Reputation Level)rod k User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 4 m 8 sec
Reputation Power: 13
Send a message via AIM to rod k
That error means you already have an invalid reference in data inside the table. You need to make sure existing data is valid for ANY new constraint, FK, check, whatever, BEFORE applying the constraint.

Reply With Quote
  #3  
Old October 3rd, 2003, 09:23 AM
metaBarf metaBarf is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 373 metaBarf User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 49 m 6 sec
Reputation Power: 6
could you avoid this problem by doing a pg_dump to a file with just the insert statements for that table, then apply the constraint then move the data back? U should know the constraints ri when you make the tables in an ideal world but that's why they make the dump tool.

Reply With Quote
  #4  
Old October 3rd, 2003, 09:34 PM
jjoske jjoske is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2001
Posts: 286 jjoske User rank is Lance Corporal (50 - 100 Reputation Level)jjoske User rank is Lance Corporal (50 - 100 Reputation Level)jjoske User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 3 Days 5 h 24 m 46 sec
Reputation Power: 7
There is only a little bit of test data in there, easier to do fix it up by hand. Thanks for the help.

John Joske

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesPostgreSQL Help > unable to create a foreign key


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT