Results 1 to 4 of 4

Thread: Foreign key error

  1. #1
    yere is offline Newbie Net Builder
    Join Date
    Jul 2009
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Foreign key error

    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 issue.

  2. #2
    moonshaneisle is offline Newbie Net Builder
    Join Date
    Jul 2009
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  3. #3
    ZeroGensis is offline Newbie Net Builder
    Join Date
    Jul 2009
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  4. #4
    moonshaneisle is offline Newbie Net Builder
    Join Date
    Jul 2009
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

Similar Threads

  1. [WTB] Foreign Currency Articles
    By elbandelero in forum Content
    Replies: 1
    Last Post: 22 November, 2010, 18:04 PM
  2. How to install GeoIP and block foreign traffic
    By Will.Spencer in forum Web Proxies
    Replies: 3
    Last Post: 6 December, 2009, 03:35 AM
  3. Foreign Keyboards + Captcha
    By Kovich in forum Tech-Talk
    Replies: 3
    Last Post: 5 October, 2009, 15:11 PM
  4. Local market with foreign ccTLD
    By Szise in forum Promoting
    Replies: 6
    Last Post: 15 April, 2009, 11:43 AM
  5. Advantages if any of submitting to foreign directory
    By keynes in forum Directory Submission
    Replies: 2
    Last Post: 11 April, 2009, 22:17 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •