Cannot add or update a child row: a foreign key constraint fails

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vasil angelov
    New Member
    • Mar 2011
    • 8

    Cannot add or update a child row: a foreign key constraint fails

    Can anyone tell me what i'm doing wrong here?I'm trying to set up a one-to-one relationship between two tables, but it just doesn't work.These are my tables:
    Code:
    CREATE TABLE prices
    (
    	id INT AUTO_INCREMENT NOT NULL PRIMARY KEY ,
    	price FLOAT NOT NULL
    	
    ) ENGINE=INNODB ;
    CREATE TABLE films
    (
        film_id INT AUTO_INCREMENT NOT NULL PRIMARY KEY ,
    	name VARCHAR(30) NOT NULL,
    	FOREIGN KEY (film_id) REFERENCES  prices (id)
    	
    	
    	) ENGINE=INNODB ;
    And this is the insert statement i'm using to populate them(I'm getting the following error:
    ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`video_library `.`films`, CONSTRAINT `films_ibfk_5` FOREIGN KEY (`film_id`) RE
    FERENCES `prices` (`id`)))
    Code:
    INSERT INTO prices (price)
    VALUES  (1.99),
    		(2.99),
    		(3.99),		
    		(4.99),
    		(5.99),
    		(6.99),
    		(7.99),
    		(8.99),
    		(9.99),
    		(10.99),
    		(11.99),
    		(12.99),
    		(13.99);
    		
    INSERT INTO films (name)		
    VALUES  ('The Godfather'),
    	    ('The Godfather II'),
    		('The Shawshank redemption'),
    		('Pulp fiction'),
    		('12 Angry men'),
    		('Schindler\'s List'),
    		('Fight club'),
    		('Inception'),
    		('The green mile'),
    		('Dog Day Afternoon'),
    		('The Dark Knight'),
    		('Saving Private Ryan'),
    		('Se7en');
    Thanks in advance
  • johny10151981
    Top Contributor
    • Jan 2010
    • 1059

    #2
    Really? Godfather is cheaper than Godfatehr II?

    you need to rethink the price ;)

    anyway:

    pointer number one this is not a very smart way to design your database. do two things
    1. Remove Auto Increment constraints from the second database.
    2 and insert all the the data in table two with proper id taken from table on1

    Comment

    • vasil angelov
      New Member
      • Mar 2011
      • 8

      #3
      Thanks for the help.

      Comment

      Working...