Search code examples
mysqlforeign-keysforeign-key-relationship

Cannot add foreign key constraint


First, here's my main reference table mysql dump

-- phpMyAdmin SQL Dump
-- version 4.1.14
-- http://www.phpmyadmin.net
--
-- Host: 127.0.0.1
-- Generation Time: Nov 17, 2015 at 03:15 AM
-- Server version: 5.6.17
-- PHP Version: 5.5.12

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO"; SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; /*!40101 SET NAMES utf8 */;

--
-- Database: `j_inventory`
--

-- --------------------------------------------------------

--
-- Table structure for table `users`
--

CREATE TABLE IF NOT EXISTS `users` (   `user_id` int(10) unsigned NOT NULL AUTO_INCREMENT,   `username` varchar(200) COLLATE utf8_unicode_ci NOT NULL,   `password` varchar(500) COLLATE utf8_unicode_ci NOT NULL,  `real_password` varchar(250) COLLATE utf8_unicode_ci NOT NULL,   `role` varchar(50) COLLATE utf8_unicode_ci NOT NULL,   `full_name` varchar(250) COLLATE utf8_unicode_ci NOT NULL,   `remember_token` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,   `created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',   `updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',   `status` varchar(100) COLLATE utf8_unicode_ci NOT NULL,   PRIMARY KEY (`user_id`) ) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=44 ;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

and the second table where the hopefully the foreign key resides

-- phpMyAdmin SQL Dump
-- version 4.1.14
-- http://www.phpmyadmin.net
--
-- Host: 127.0.0.1
-- Generation Time: Nov 17, 2015 at 03:14 AM
-- Server version: 5.6.17
-- PHP Version: 5.5.12

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO"; SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; /*!40101 SET NAMES utf8 */;

--
-- Database: `j_inventory`
--

-- --------------------------------------------------------

--
-- Table structure for table `user_details`
--

CREATE TABLE IF NOT EXISTS `user_details` (   `id` int(11) NOT NULL AUTO_INCREMENT,   `phone` varchar(200) NOT NULL,   `age` int(100) NOT NULL,   `gender` varchar(50) NOT NULL,   `address` varchar(250) NOT NULL,   `course` varchar(250) NOT NULL,   `college` varchar(200) NOT NULL,   `year` int(11) NOT NULL,   `user_id` int(100) NOT NULL,   `updated_at` timestamp NOT NULL,   `created_at` timestamp NOT NULL,   PRIMARY KEY (`id`),   KEY `user_id` (`user_id`) ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

the foreign key (not set up due to mysql error "Cannot add foreign key constraint ") in the users_details is the 'user_id' and the reference table is the 'users' table where the reference key for the foreign key is the 'user_id' on the users table and I tried this

ALTER TABLE user_details ADD FOREIGN KEY fk1(user_id) REFERENCES users(user_id) ON DELETE CASCADE ON UPDATE NO ACTION;

but sadly and unfortunately it throws me an error

Error SQL query:

ALTER TABLE user_details ADD CONSTRAINT fk1 FOREIGN KEY (user_id) REFERENCES j_inventory.users(user_id) ON DELETE CASCADE ON UPDATE NO ACTION; MySQL said: Documentation

1215 - Cannot add foreign key constraint Documentation

any help, clues, ideas, suggestions, recommendations please?


Solution

  • Your user id is unsigned on the users table. Remove 'unsigned' from this field in your users table create statement and see if it works. I think what you're seeing is an error caused by a mismatch between the user_id field types on your users and user_details tables