12

I have MySQL Server 5.1.62 installed on production server. I am monitoring mysql server's error log file every day and suddenly I found below error in my error log file.

InnoDB: Cannot delete/update rows with cascading foreign key constraints that exceed max depth of 250
Please drop excessive foreign constraints and try again

I have a database structure with primary key - foreign key relationships with proper update/delete actions and I need to delete data of child tables if the data in parent table deleted by application or manually (backend).

I had googled this issue but I can't find proper solution. How can I resolve this issue?

Saharsh Shah
  • 28,687
  • 8
  • 48
  • 83

3 Answers3

3

Have a look at this link - Cascade Delete results in "Got error -1 from storage engine". There is a suggestion.

Also, as a solution you may try to do it without ON DELETE CASCADE option, just use DELETE statement that removes records from some tables (multiple-table syntax).

Devart
  • 119,203
  • 23
  • 166
  • 186
3

The picture of a schema isn't very useful, because it doesn't show any cascading declarations. For example, if deletes are supposed to cascade from tbl_indentmaster to tbl_tepdetails, but deletes are not supposed to cascade from tbl_tepdetails to tbl_tepnoting, then then I'd expect some deletes to fail. (But with a different error message.)

If there is a circular referential constraint that's causing this, I'd expect it to be caused in part by a cascading reference from tbl_indentmaster to tbl_tepdetails. You might want to try dropping that foreign key constraint for testing. Do that on a tset sserver, not on the production server.

If this started suddenly, and your database worked correctly before, I'd first think about

  • restoring the database from backup, or
  • restoring the schema from backup, and reloading the current data, or
  • checking out the current version and rebuilding the database. (You do have the database schema under version control, don't you?)

I'll assume you don't have a good backup, and that you don't have your schema under version control.

Are you starting with a good database? Run mysqlcheck. Read that documentation carefully. Don't --repair before you have a tested, good backup.

Assuming that your database is good, that cascading deletes ought to work correctly in your database, and that your Google skills are good, I think your best start is to

  • install MySQL 5.5 or 5.6 on a test server,
  • load your database onto that test server, and
  • see whether you can reproduce that specific error.

To load your database onto the test server, dump the contents using mysqldump. Don't copy files at the filesystem level--one or more of them might be corrupt.

Although this might not resolve your issue, it might tell you exactly where the issue is. If it works correctly, you know the problem is probably related to the server version, and that it might be resolved with a version upgrade.

Mike Sherrill 'Cat Recall'
  • 91,602
  • 17
  • 122
  • 185
  • I have updated mysql version to 5.5 but still I am facing the same issue – Saharsh Shah Dec 26 '12 at 11:15
  • In my schema design all the relationship created with cascade delete. And till mow i am not deleting any rows from db but now i want to delete past data so i got this error – Saharsh Shah Dec 26 '12 at 14:54
  • Did you drop the foreign key constraint between tbl_indentmaster and tbl_tepdetails on a test server, then try to delete rows from tbl_indentmaster? – Mike Sherrill 'Cat Recall' Dec 26 '12 at 16:08
  • It will work if i remove constraint from db. But i can't do that. As per my understanding the error occurs because of exceed depth of constraint as if i delete 50 rows from indentmaster table it automatically deletes more than 1000 rows from different tables so it crosses the depth of cascade delete – Saharsh Shah Dec 26 '12 at 17:40
  • Your understanding is wrong; the number of rows is irrelevant. The problem is *probably* related to the number of tables you're cascading the delete to. And that's *probably* because you have a circular reference. (The delete cascades back to the table it started in.) If it works when you drop the FK constraint between tbl_indentmaster and tbl_tepdetails, put *that* constraint back, and drop the FK constraint between tbl_tepdetails and tbl_tepmapbidder. Repeat until you isolate the problem, which I expect will be a circular FK reference. – Mike Sherrill 'Cat Recall' Dec 26 '12 at 17:48
1

I agree with the original answers by @Devart and @Catcall here but I'd like to add a few things after exchanging a few comments with the OP.

First, I have reduced the schema image representation to only the tables that are affected by a DELETE query on tbl_indentmaster.

From what I could see there are no circular FK references in this schema diagram.

Also, the OP ran the following query:

DELETE FROM tbl_indentmaster WHERE indentId IN (1,2,3,4,5,6,...,150,151,155,156,....)

That's an aweful lot of rows to delete. On enquiring further the OP claims that the query works for smaller subsets of indentId's.

From this I think we can take two possibilities:

  1. There's a bug in MySQL (highly unlikely but possible) which causes large queries with CASCADE DELETE like yours to fail. Note I am suggesting the possibility of a new bug not the one [posted already][2]. Ideally the number of rows to delete should not matter.
  2. There is a particular indentId entry within tbl_indentmaster which is causing the entire query to fail.

I'd suggest that you first try to diagnose the issue considering point (2) is the actual culprit. You can break the DELETE query into smaller chunks and find the offending id's.

If this script is something that has to be periodically executed through code (in a larger application) then you should consider executing the query in smaller chunks there as well (probably 15 id's per query is a good start IMO). In addition to doing this I'd suggest logging errors with offending id's in a log file so you know exactly which entries are failing.

Saharsh Shah
  • 28,687
  • 8
  • 48
  • 83
Tanzeel Kazi
  • 3,797
  • 1
  • 17
  • 22
  • I have already grouped sata in small chunks and deleted it. I want to fing root cause of this error. I also think this is limitation of mysql. – Saharsh Shah Dec 27 '12 at 12:15