Question

There is a problem to create a table with foreign_key. Can you please help me to solve it. The error is: SQL query:

ALTER TABLE users ADD CONSTRAINT FK_users FOREIGN KEY ( groupname_id ) REFERENCES groups( group_id ) ON UPDATE CASCADE ON DELETE CASCADE ;

MySQL said: Documentation

1215 - Cannot add foreign key constraint

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) unsigned NOT NULL,
  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;
Was it helpful?

Solution

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;

OTHER TIPS

The problem is that your groupname_id column is unsigned. Try this:

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,
  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;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top