Bug #29624 | ALTER TABLE with AUTO_INCREMENT does not respect foreign key constraints | ||
---|---|---|---|
Submitted: | 8 Jul 2007 2:08 | Modified: | 17 Jul 2007 19:30 |
Reporter: | Vincent Huisman | Email Updates: | |
Status: | Closed | Impact on me: | |
Category: | MySQL Server: Documentation | Severity: | S2 (Serious) |
Version: | 5.0.46-BK,5.1.20-beta/5.1.11-beta/4.0.27/? | OS: | Any |
Assigned to: | Paul DuBois | CPU Architecture: | Any |
[8 Jul 2007 2:08]
Vincent Huisman
[8 Jul 2007 8:40]
Valeriy Kravchuk
Thank you for a bug report. Verified just as described with latest 5.0.46-BK on Linux. I think, ALTER TABLE should just not change 0 to anything for existing rows. But if it does, then change should indeed be apllied to correspondent rows based on foreign key constraint.
[9 Jul 2007 13:36]
Heikki Tuuri
The bug is due to the fact that InnoDB does not check if ALTER TABLE changes the values of some existing column. A similar bug occurs if you modify the column type so that MySQL has to 'truncate' the values in it. Fortunately, SQL mode = traditional nowadays blocks that other bug. The behavior where making a colum auto-inc changes the values of the column is dubious. It would be more logical that MySQL would keep the value 0, so that this bug would not occur.
[10 Jul 2007 14:57]
Bugs System
A patch for this bug has been committed. After review, it may be pushed to the relevant source trees for release in the next version. You can access the patch from: http://lists.mysql.com/commits/30612 ChangeSet@1.2527, 2007-07-10 17:57:10+03:00, gkodinov@magare.gmz +3 -0 Bug #29624: ALTER TABLE with AUTO_INCREMENT does not respect foreign key constraints ALTER TABLE may create a temporary table; copy the data from the original table and then redirect the original table to the temp table. It copies the data using the internal equivalent of INSERT ... SELECT ... However INSERT into an AUTO_INCREMENT field treats 0 and NULL as "magic" values and substitutes them with the next auto-increment value. This is a welcome effect if the AUTO_INCREMENT field is newly added. But this substitution should not be done if the field that has the AUTO_INCREMENT flag in the modified table existed in the original table. Fixed by disabling the substitution of magic values when copying the data in ALTER TABLE for columns that are not newly added.
[11 Jul 2007 15:20]
Bugs System
A patch for this bug has been committed. After review, it may be pushed to the relevant source trees for release in the next version. You can access the patch from: http://lists.mysql.com/commits/30712 ChangeSet@1.2527, 2007-07-11 18:20:07+03:00, gkodinov@magare.gmz +3 -0 Bug #29624: ALTER TABLE with AUTO_INCREMENT does not respect foreign key constraints ALTER TABLE may create a temporary table; copy the data from the original table and then redirect the original table to the temp table. It copies the data using the internal equivalent of INSERT ... SELECT ... However INSERT into an AUTO_INCREMENT field treats 0 and NULL as "magic" values and substitutes them with the next auto-increment value. This is a welcome effect if the AUTO_INCREMENT field is newly added. But this substitution should not be done if the field that has the AUTO_INCREMENT flag in the modified table existed in the original table. Fixed by disabling the substitution of magic values when copying the data in ALTER TABLE for columns that are not newly added.
[13 Jul 2007 15:58]
Sergei Golubchik
There are many data modifications that can happen during ALTER TABLE. Auto_increment and truncation are examples. Others are - character set conversions, extending (not truncating) BINARY column (binary columns are '\0'-padded, and zero bytes are not stripped on comparison), changing NULL column to NOT NULL, etc. It's InnoDB bug (or undocumented limitation), that it doesn't expect these data modifications, and they break foreign key constraints.
[16 Jul 2007 13:41]
Heikki Tuuri
This has to be documented both in MySQL's ALTER TABLE and InnoDB's FOREIGN KEY sections in the manual. Paul, please add there: "If ALTER TABLE changes column values, for example, because a column is truncated, InnoDB's FOREIGN KEY contraint checks do not notice possible violations caused by changing the values." This behavior itself is a bug, of course. When/if MySQL gets its own foreign key constraints, this becomes fixed.
[17 Jul 2007 18:34]
Paul DuBois
I have added the notes requested by Heikki. Returning this report to NDI status because no version numbers were given for the patch that was made. (Or perhaps the status really should be Patch pending or queued?)
[17 Jul 2007 19:30]
Paul DuBois
No patch was applied. Closing this report.
[9 Aug 2013 6:34]
Sagar Jaisur
If You have table with auto increment and You want to add field at begin with id ? At that no need to drop table simply follow the four step ? => Remove the Auto incremnt Of that field => update All ids with increment of 1 in descending order => insert your recode with your desire id. => and add your field auto increment.