Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

The Database Forum


You are currently viewing our The Database Forum as a guest. Please register to participate.
Login



Reply
Old 11-09-2008, 10:56 PM Foreign key error
Junior Talker

Posts: 1
Name: K Harris
Trades: 0
I am getting this error when trying to add any data in the myphpadmin.

#1452 - Cannot add or update a child row: a foreign key constraint fails (`artdb/image`, CONSTRAINT `image_ibfk_1` FOREIGN KEY (`artid`) REFERENCES `artist` (`artid`))

I am using only one field to input so I know that I am using a valid artid.

After playing with around it seems as though I can only enter 1 artid and the rest gets kicked out, but that is only if I go directly thru the Insert on myphpadmin. If I re-add the tables and go with a different artid it works only for that one.

Here are the 2 tables I am working with -
Code:
CREATE TABLE artist (artid varchar(11) NOT NULL PRIMARY KEY,
 
 fullName tinytext Not Null,
 firstName tinytext NULL,
 middleName tinytext NULL,
 lastName tinytext NULL,
 otherName tinytext NULL,
 called tinytext NULL,
 dob_xt tinytext NULL,
 dob tinytext NULL,
 dod_xt tinytext NULL,
 dod tinytext,
 nationality tinytext,
 artform tinytext,
 movid_1 varchar(7),
 movid_2 varchar(7),
 movid_3 varchar(7),
 medid_1 varchar(7),
 medid_2 varchar(7),
 medid_3 varchar(7),
 genid_1 varchar(7),
 genid_2 varchar(7),
 genid_3 varchar(7),
 perid varchar(7),
 bio mediumtext,
 keywords tinytext,
 notes text,
 verified BOOL);
 
CREATE TABLE image (imgid varchar(11) NOT NULL PRIMARY KEY,
 artid varchar(11) NOT NUll,
 title tinytext,
 datext_1 tinytext,
 date tinytext,
 medid varchar(7),
 size_inch tinytext,
 size_cm tinytext,
 musid varchar(7),
 movid varchar(7),
 genid varchar(7),
 perid varchar(7),
 bio text,
 keywords text,
 notes text,
 verified BOOL,
 FOREIGN KEY (artid) REFERENCES artist (artid)
 );
I understand a bit of the error msg but not this part - `image_ibfk_1`

I am not really sure that I need to use a Foreign Key. But I can't get a query to work and was thinking this may be the problem.
harklt is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 11-10-2008, 05:24 AM Re: Foreign key error
stoot98's Avatar
Ultra Talker

Posts: 427
Name: Stuart
Location: Glasgow, Scotland
Trades: 0
I think that "image_ibfk_1" is the internally generated name given to the Foreign key constraint.

You must have a row in the artist table with an artID equal to the artID of the row you are inserting into the image table.
stoot98 is offline
Reply With Quote
View Public Profile
 
Old 11-10-2008, 05:46 AM Re: Foreign key error
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Quote:
You must have a row in the artist table with an artID equal to the artID of the row you are inserting into the image table.
It's exactly that.
The role of the foreign key is to ensure that the information you enter correspond to something that is already existing.

In your case, the insert into the table image has an artId that does not exist in the artist table, making the exception fire.
You have to have an artist declared, before you can refer to it.
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 11-11-2008, 02:11 PM Re: Foreign key error
Learning Newbie's Avatar
Defies a Status

Latest Blog Post:
Astounding Republican Paranoia
Posts: 5,662
Name: John Alexander
Trades: 0
You don't need an FK if you want to allow your data to be corrupted.

A foreign key is an object, and as such, it needs a name. You didn't give it one, so the database did. Now, with its name, you can alter or drop it.
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
Learning Newbie is offline
Reply With Quote
View Public Profile
 
Old 11-17-2008, 01:45 PM Re: Foreign key error
Learning Newbie's Avatar
Defies a Status

Latest Blog Post:
Astounding Republican Paranoia
Posts: 5,662
Name: John Alexander
Trades: 0
Quote:
Originally Posted by gadgeteeer View Post
to trace it your profiler if youre using MSSQL... it will give where you got the error. that will be a good starting point of our hunting since do dont know you FK's name.
good hunting!
The key's name is image_ibfk_1, and harklt is using MySQL, not Microsoft, so Profiler isn't an option.
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
Learning Newbie is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Foreign key error
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.21907 seconds with 12 queries