0

Please help I've tried everything and every single solution on StackOverflow and other sites on Internet.

I've created 2 tables (categories) and (items) and inserted this row into categories:

INSERT INTO categories (Name, Description, Ordering, Visibility, Allow_Comments, Allow_Ads)
VALUES ('Wheel', 'Custom', 1, 0, 0, 0)

Successfully inserted.

Then created the Foreign key:

ALTER TABLE items
ADD CONSTRAINT Cat_1
FOREIGN KEY(Cat_ID)
REFERENCES categories(ID)
ON UPDATE CASCADE
ON DELETE CASCADE;

Successful Created.

The problem is when I'm trying to insert rows into items got the error message.

INSERT INTO items (Name, Description, Type, Color, Certified, Quantity, Date, Price)
VALUES ('Test', 'Test', 'Wheel', 'Test', 'Test', 1, NOW(), 100);

1452 Cannot add or update a child row: a foreign key constraint fail

Tried to add images but didn't work. I've double checked the values.

Katie
  • 2,594
  • 3
  • 23
  • 31
M Jandali
  • 1
  • 1
  • Because you don't add the Foreign key to it – Baracuda078 Jun 15 '20 at 12:16
  • What do you mean, I already did. – M Jandali Jun 15 '20 at 12:28
  • 1
    In your insert query, you did not add the foreign key of Wheel – Baracuda078 Jun 15 '20 at 12:54
  • Does this answer your question? [Cannot add or update a child row: a foreign key constraint fails](https://stackoverflow.com/questions/5005388/cannot-add-or-update-a-child-row-a-foreign-key-constraint-fails) – SternK Jun 15 '20 at 12:58
  • @Baracuda078 Thanks for your answer, but how to add foreign key while inserting the query? I already created a custom column in child table. – M Jandali Jun 15 '20 at 13:36
  • @SternK Thank you I already tried everything in that topic, My problem is not creating the foreign key I successfully created it but it wont let me insert query in child table after that. – M Jandali Jun 15 '20 at 13:37
  • By the way, what database do you use? – SternK Jun 15 '20 at 13:47
  • It's MySQL ..... – M Jandali Jun 15 '20 at 14:05
  • Clarify via edits, not comments. Give a [mre]. – philipxy Jun 15 '20 at 22:29
  • Welcome to Stack Overflow. When you say "I've tried everything and every single solution on StackOverflow" it would be helpful to tell us what you've already tried, so that we don't suggest things you've already done. Making that statement also tells us that it isn't any of the obvious answers, but doesn't give us a lot of information to make good suggestions. – Isaac Bennetch Jun 16 '20 at 15:12

1 Answers1

0

I found the problem maybe it helps someone, I'm using the plugin "editable Select" which replaced the select form, and insert the name in database not the value, and since the column is integer it wont post text into it.

M Jandali
  • 1
  • 1
  • 1
    Whatever system this works in, it's not in the question. (And it should have been.) Also this is practiclally unintelligible. Use enough words & sentences to be clear, don't cram everything into one word or sentence. – philipxy Jun 15 '20 at 22:28