Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1017485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:42:44+00:00 2026-05-16T10:42:44+00:00

This is what I’m trying to do: I have 2 tables… CREATE TABLE `parent`

  • 0

This is what I’m trying to do:

I have 2 tables…

CREATE TABLE `parent` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `data` text,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

CREATE TABLE `child` (
  `parent_id` int(11) DEFAULT NULL,
  `related_ids` int(11) DEFAULT NULL,
  KEY `parent_id` (`parent_id`),
  KEY `related_ids` (`related_ids`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

And then a constraint:

ALTER TABLE `parent` ADD FOREIGN KEY (`id`) REFERENCES `child` (`parent_id`);

As you can see the table parent has an auto-incremented primary key “id”, which is also being used as a foreign key for the child table.

Now I want to insert a record in the parent table, like this:

INSERT INTO parent SET DATA="abc";

And it fails with error:

Cannot add or update a child row: a
foreign key constraint fails
(myschema.parent, CONSTRAINT
parent_ibfk_1 FOREIGN KEY (id)
REFERENCES child (parent_id))

I understand that it fails because it doesn’t find a referred record in the child table. If I start by creating a record in the child table, set it’s parent_id to 1, then reset the auto-increment counter of the parent table (so that the next insert will have id = 1), it works! But that’s not a solution.

I don’t see the utility of the insert blocking if there is no related row in the child table…

I’m just trying to do a one-to-many relationship…

(I know I can use JOIN, but I’m trying to use table relations, for data integrity and also as metadata for PHP)

  • 1 1 Answer
  • 2 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T10:42:45+00:00Added an answer on May 16, 2026 at 10:42 am

    It looks like you have the referencing and referenced tables in reverse. You may want to do:

    ALTER TABLE `child ` ADD FOREIGN KEY (`parent_id`) REFERENCES `parent` (`id`);
    

    You can also define the foreign key in the CREATE TABLE statement, as follows:

    CREATE TABLE `parent` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `data` text,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
    
    CREATE TABLE `child` (
      `parent_id` int(11) DEFAULT NULL,
      `related_ids` int(11) DEFAULT NULL,
      KEY `parent_id` (`parent_id`),
      KEY `related_ids` (`related_ids`),
      FOREIGN KEY (`parent_id`) REFERENCES `parent`(`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    

    Test case:

    INSERT INTO parent (`data`) VALUES ('test data 1');
    Query OK, 1 row affected (0.01 sec)
    
    INSERT INTO parent (`data`) VALUES ('test data 2');
    Query OK, 1 row affected (0.01 sec)
    
    INSERT INTO child (`parent_id`, `related_ids`) VALUES (1, 100);
    Query OK, 1 row affected (0.01 sec)
    
    INSERT INTO child (`parent_id`, `related_ids`) VALUES (2, 100);
    Query OK, 1 row affected (0.01 sec)
    
    INSERT INTO child (`parent_id`, `related_ids`) VALUES (3, 100);
    ERROR 1452 (23000): Cannot add or update a child row: 
      a foreign key constraint fails 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
This should be a simple one: I have an observableArray object called To in
This is my code: <CheckBox android:id=@+id/sprint_checkbox android:layout_width=fill_parent android:layout_height=wrap_content android:text=@string/sprint_game /> <CheckBox android:id=@+id/marathon_checkbox android:layout_width=fill_parent android:layout_height=wrap_content
Basically, what I'm trying to create is a page of div tags, each has
For some reason, after submitting a string like this Jack’s Spindle from a text
This is my first attempts at creating a data-mart/warehouse and I am a little
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
This should not be so complicated: I am using Easiest Tooltip and Image Preview
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.