Jump to content

[SOLVED] Foreign key constraint failure


harkly

Recommended Posts

I added a Foreign Key to a table an am trying to import data thru phpmyadmin

 

this is the error

 

#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`))

 

these are my tables :

 

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 have verified that there are no empty cells in the image table but there are a bunch of repeats, could that be an issue? I am thinking not. There isn't much info on the web for this. I understand a bit of the error but not this part `image_ibfk_1`  so I can't move on.

 

 

 

Link to comment
Share on other sites

You should take care in which order you're inserting your tables (and data). If you insert data that references to rows in other table, while the other table is still empty you will obviously get errors.

 

I found it useful to remove foreign keys before importing data, and then recreate them.

 

 

Link to comment
Share on other sites

Its definitely not that I am importing an image file that has no artist record. Verified this multiply times.

 

I also tried the import first then create FK but got the same error when creating the FK.

 

Can you tell me why I would need a Foreing Key? In very simple terms. I have read a lot about them but I don't really think I need it if each table has a column conntecting each other.

Link to comment
Share on other sites

A foreign key constraint enforces integrity of the data and serves two purposes

 

1 ) it stops you inserting a child record (image) that has no parent (artist)

 

2 ) it stops you deleting a parent if that parent has children (unless you have cascaded deletes, in which case deleting the parent also deletes the child records)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.