www.webdeveloper.com
Results 1 to 3 of 3

Thread: foreign key constraints

  1. #1
    Join Date
    Nov 2012
    Location
    Pakistan.
    Posts
    8

    foreign key constraints

    Hey every one, hope all of you are fine.
    please tell me when a foreign key constraints fails.
    I have and error "Cannot add or update a child row: a foreign key constraint fails"

    when i enter data in two tables one is user and other is user_info that is connected with user table with foreign key, but user accept data and user_info do not accept you can see it in below code i have pasted some data from .sql dump file. at the end of user table there is some insertion query but not for user_info.

    Code:
    Table structure for table `user`
    --
    
    CREATE TABLE IF NOT EXISTS `user` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `username` varchar(30) NOT NULL,
      `password` varchar(30) NOT NULL,
      `email` varchar(30) NOT NULL,
      `date` datetime NOT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
    
    --
    -- Dumping data for table `user`
    --
    note:this table is accepting data.
    INSERT INTO `user` (`id`, `username`, `password`, `email`, `date`) VALUES
    (1, 'adnan_akbar', 'asdf', 'atta.akbar@yahoo.com', '2012-12-03 12:41:05'),
    (2, 'aliahmad', 'sd', 'a2323_yad@gsdfdsf.comsd', '2012-12-03 13:02:28');
    
    -- --------------------------------------------------------
    
    --
    -- Table structure for table `user_info`
    --
    
    CREATE TABLE IF NOT EXISTS `user_info` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `parent_id` int(11) NOT NULL,
      `firstname` varchar(10) NOT NULL,
      `lastname` varchar(10) NOT NULL,
      `birth` int(20) NOT NULL,
      `bio` varchar(160) NOT NULL,
      `city` varchar(30) NOT NULL,
      `country` varchar(30) NOT NULL,
      `phone` int(16) NOT NULL,
      `extension` int(20) NOT NULL,
      PRIMARY KEY (`id`),
      KEY `parent_id` (`parent_id`)
    ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;

  2. #2
    Join Date
    Dec 2012
    Location
    Lahore, Pakistan
    Posts
    10

    Problem might be inside your insert queries

    i tried the following insert queries and they worked.

    Code:
    INSERT INTO `user` (`id`, `username`, `password`, `email`, `date`) VALUES
    (1, 'adnan_akbar', 'asdf', 'atta.akbar@yahoo.com', '2012-12-03 12:41:05'),
    (2, 'aliahmad', 'sd', 'a2323_yad@gsdfdsf.comsd', '2012-12-03 13:02:28');
    
    INSERT INTO `user_info` (`parent_id`, `firstname`, `lastname`, `birth`, `bio`,`city`,`country`,`phone`,`extension`) VALUES
    (1, 'firstname1', 'lastname1', 30, 'bio 1','city1','country1',11,111),
    (2, 'firstname2', 'lastname2', 32, 'bio 2','city2','country2',22,222);

  3. #3
    Join Date
    Nov 2012
    Location
    Pakistan.
    Posts
    8

    foreign key constraint problem

    Thanks a lot brother but i have solved that one actually i was not taking id form previous query using mysql_insert_id function that was causing problem.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles