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

  • Home
  • SEARCH
  • 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 261643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T22:26:57+00:00 2026-05-11T22:26:57+00:00

I’m having some trouble using constraints correctly. I have three tables, ‘item’, ‘store’ and

  • 0

I’m having some trouble using constraints correctly.

I have three tables, ‘item’, ‘store’ and ‘link_item_store’.
An Item can be in one or many stores, and one or many stores can have an item.
Since this is a many to many relationship, I’m using ‘link_item_store’ to normalize.

If I delete an item, I have to remove all instancs of that item in the ‘link_item_store’ table. And the same goes for store. This is my table and it’s constraints:

CREATE TABLE `link_item_store` (
  `fk_storeID` int(11) unsigned NOT NULL,
  `fk_itemID` int(11) unsigned NOT NULL,
  PRIMARY KEY  (`fk_storeID`,`fk_itemID`),
  KEY `fk_storeID` (`fk_storeID`),
  KEY `fk_itemID` (`fk_itemID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_danish_ci;

ALTER TABLE `link_item_store`
  ADD CONSTRAINT `link_item_store_ibfk_2` FOREIGN KEY (`fk_itemID`) REFERENCES `link_item_store (`fk_itemID`) ON DELETE CASCADE,
  ADD CONSTRAINT `link_item_store_ibfk_1` FOREIGN KEY (`fk_storeID`) REFERENCES `link_item_store` (`fk_storeID`) ON DELETE CASCADE;

And heres is an example list:

fk_storeID, fk_itemID, itemName, storeName
11  7277    item 1  Test store
11  7278    item 2  Test store
11  7280    item 3  Test store
12  7277    item 1  Test store 2
12  7278    item 2  Test store 2
12  7290    item 4  Test store 2
35  7295    item 4  Test store 4
35  7299    item 5  Test store 4
35  7300    item 6  Test store 4
35  7302    item 7  Test store 4

My problem is that if I delete ‘item 7’ from ‘item’ table, ALL references are deleted from link_item_store. 🙁

What to do?

Update:
By having these two constraints on the table, I’m not able to insert new data to the table. I get the following error message:

Unable to query local database <b>Cannot add or update a child row: a foreign 
key constraint fails ( `link_item_store`, CONSTRAINT 
`link_item_store_item_ibfk_1` FOREIGN KEY (`fk_storeID`) REFERENCES 
`link_item_store` (`fk_storeID`) ON DELETE CASCADE)</b><br> INSERT INTO 
link_item_store (fk_storeID, fk_itemID) VALUES ('11', '7295')

Update 2 – Solution:
Michael pointed out to me that the ADD CONSTRAINT didn’t look right. On a closer look, I saw that he was right. This is the code that is generated by PhpMuAdmin upon adding constraints:

ALTER TABLE `link_item_store` ADD FOREIGN KEY ( `fk_itemID` ) REFERENCES `mydatabase`.`link_item_store` (`fk_itemID`) ON DELETE CASCADE ;

As you can see, the foreign key is reffering to itself! This defenetily have to be a bug in PhPMyAdmin.

The solution was to change reference. This works and I no longer have problems adding new records to the table:

ALTER TABLE `link_item_store` ADD FOREIGN KEY ( `fk_itemID` ) REFERENCES `mydatabase`.`item` (`id`) ON DELETE CASCADE ;
  • 1 1 Answer
  • 0 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-11T22:26:57+00:00Added an answer on May 11, 2026 at 10:26 pm

    The syntax of your foreign keys seems unusual to me.

    Instead of:

    ADD CONSTRAINT `link_item_store_ibfk_2` FOREIGN KEY (`fk_itemID`) REFERENCES `link_item_store` (`fk_itemID`) ON DELETE CASCADE
    

    Try:

     ADD CONSTRAINT `link_item_store_ibfk_2` FOREIGN KEY (`fk_itemID`) REFERENCES item (`itemID`) ON DELETE CASCADE
    

    (Assuming itemID is the name of the correct column on the item table)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 237k
  • Answers 237k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer String.gsub should do the trick. Quoting docs: gsub(pattern, replacement) →… May 13, 2026 at 6:41 am
  • Editorial Team
    Editorial Team added an answer Your MakePoring method is public, while your Enemigo class is… May 13, 2026 at 6:41 am
  • Editorial Team
    Editorial Team added an answer Tables that store changes when the main table changes are… May 13, 2026 at 6:41 am

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I want use html5's new tag to play a wav file (currently only supported
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I've got a string that has curly quotes in it. I'd like to replace
In order to apply a triggered animation to all ToolTip s in my app,

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.