As I mentioned in my comment, the problem is that groupname_id
on the users
table is unsigned, while the group_id
on the groups
table is signed.
It was a hunch, so I tested it, and just now successfully added and altered these tables on my test
database. MySQL 5.6.16 on Windows.
CREATE TABLE IF NOT EXISTS `groups` (
`group_id` int(11) NOT NULL AUTO_INCREMENT,
`group_level` varchar(100) DEFAULT NULL,
`groupname` varchar(100) NOT NULL,
PRIMARY KEY (`group_id`),
KEY idx_groupname (groupname)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=64 ;
CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`firstname` varchar(100) DEFAULT NULL,
`lastname` varchar(100) DEFAULT NULL,
`username` varchar(100) NOT NULL,
`password` char(100) NOT NULL,
`phone` varchar(100) DEFAULT NULL,
`is_active` int(1) NOT NULL DEFAULT '0',
`groupname_id` int(11) NOT NULL, -- should be signed, not unsigned, just like above
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=46 ;
ALTER TABLE users
ADD CONSTRAINT FK_users
FOREIGN KEY (groupname_id) REFERENCES groups(group_id)
ON UPDATE CASCADE
ON DELETE CASCADE;