CMS MADE SIMPLE FORGE

Frontend Users

 

[#10934] Foreign key restraints prevent upgrade altering column to auto_increment

avatar
Created By: stephen cooper (scooper)
Date Submitted: Thu Mar 10 11:12:06 -0500 2016

Assigned To: Robert Campbell (calguy1000)
Version: 1.31
CMSMS Version: v2.1.2
Severity: Minor
Resolution: Fixed
State: Closed
Summary:
Foreign key restraints prevent upgrade altering column to auto_increment
Detailed Description:
Right - slightly complicated this one but were getting errors adding and
deleting users after a recent update:

Cannot delete or update a parent row: a foreign key constraint fails 
(`cms_module_feusers_history`, CONSTRAINT `cms_module_feusers_history_ibfk_1`
FOREIGN KEY (`userid`) REFERENCES `cms_module_feusers_users` (`id`))

Comparing our tables in PhpMyAdmin  with a fresh install of FEU I saw that the
id col for the users table was not AUTO_INCREMENT.

When I tried to change it directly to AUTO_INCREMENT  in phpMyAdmin I got the
similar error:

Query error:
#1833 - Cannot change column 'id': used in a foreign key constraint
'cms_module_feusers_belongs_ibfk_1' of table 'cms_module_feusers_belongs'

Soooo - I then deleted all the foreign key restraints manually, updated users.id
(and the other cols that now need to be AUTO_INCREMENT) and then reapplied the
foreign key restraints (as specified in the install file). That seemed to sort
everything out ok.

Looking at the module.upgrade code I reckon there's a problem with the order
you're updating things which might be causing this;

In the less than 1.30 conditional you add the foreign keys first (at about line
465) and then try to update the feusers_users id col to auto_increment at about
line 491. Cos you have it wrapped in a try / catch then it won't throw an error.

It may be that this depends on what the server has set for FOREIGN_KEY_CHECKS
but at least on my fairly generic install it was certainly failing.

I reckon though that if you just swapped the order round then that might solve
things. So just alter the column to auto_increment first and then add the
foreign keys once that has been done. I can't see any obvious reason we can't
just do that.

Cool. Probably not a common issue, but it took me most of the morning to work
out, so if we can prevent it happening to someone else that'd be good. Let me
know if you need any more info.

Thanks as always.

s.



History

Comments
avatar
Date: 2016-03-24 15:06
Posted By: Robert Campbell (calguy1000)

should be fixed in svn, thanks
      
Updates

Updated: 2016-07-26 18:35
state: Open => Closed

Updated: 2016-03-24 15:06
resolution_id: => 7