Contraint sql error: #1005 - Can't create table - mysql

Error
I have a working site in one server. I decided to move it to another company. I exported the database with phpmyadmin and uploaded in the new server. Everytime I try to import the database I receive this error:
SQL query:
--
-- Constraints for table `seller_cart`
--
ALTER TABLE `seller_cart`
ADD CONSTRAINT `seller_cart_ibfk_1`
FOREIGN KEY ( `subscription` )
REFERENCES `subscription` ( `id` ) ,
ADD CONSTRAINT `seller_cart_ibfk_2`
FOREIGN KEY ( `user` )
REFERENCES `users` ( `user_id` )
ON DELETE CASCADE
ON UPDATE CASCADE ,
ADD CONSTRAINT `seller_cart_ibfk_3`
FOREIGN KEY ( `featured_item` )
REFERENCES `featured_item` ( `item` )
ON DELETE CASCADE
ON UPDATE CASCADE ;
MySQL said: Documentation
#1005 - Can't create table 'project123.#sql-12050_1d' (errno: 150) (Details...)
HERE'S THE STRUCTURE OF seller cart:
--
-- Table structure for table `seller_cart`
--
CREATE TABLE IF NOT EXISTS `seller_cart` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`user` bigint(20) NOT NULL,
`subscription` bigint(20) DEFAULT NULL,
`description` varchar(200) COLLATE utf8_unicode_ci DEFAULT NULL,
`item_listings` text COLLATE utf8_unicode_ci,
`featured_item` bigint(20) DEFAULT NULL,
`quantity` int(2) NOT NULL,
`price` float NOT NULL,
PRIMARY KEY (`id`),
KEY `user` (`user`),
KEY `subscription` (`subscription`),
KEY `featured_item` (`featured_item`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;
What could be the problem? I just exported and imported the samedatabase. Even tried on a localhost and the same problem.

This error is thrown when you are trying to refer a column which is not indexed.
Check if
subscription ( id )
users ( user_id )
featured_item ( item )
have valid indexes defined. If not, define them and run your ALTER ... statement.
Refer To: Rules for Foreign Key Constraints:
InnoDB and FOREIGN KEY Constraints

Related

Error 1215 on adding foreign key. Help me with what I haven't tried

I'm trying to set a foreign key relationship between two existing table but for some reason which I'm missing I keep getting this error 1215.
The relevant fields of the two tables are:
CREATE TABLE `approvals` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`id_contract` int(11) NOT NULL,
`id_user` int(11) NOT NULL,
`lang` varchar(8) NOT NULL,
`request_ts` datetime NOT NULL,
`version` int(11) NOT NULL,
-- more nullable columns here with no indexing/relationships
PRIMARY KEY (`id`),
KEY `approval_id_user` (`id_user`),
KEY `version` (`version`,`lang`,`id_contract`),
CONSTRAINT `approval_id_user` FOREIGN KEY (`id_user`)
REFERENCES `users` (`iduser`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=1079 DEFAULT CHARSET=utf8
CREATE TABLE `version` (
`idversion` int(11) NOT NULL,
`idcontract` int(11) NOT NULL,
`language` varchar(8) NOT NULL,
PRIMARY KEY (`idversion`,`idcontract`,`language`),
KEY `fk_version_contracts_idx` (`idcontract`),
CONSTRAINT `fk_kidversion_contracts` FOREIGN KEY (`idcontract`)
REFERENCES `contracts` (`id_contract`)
ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8
What have I tried
First thing I checked is the persistency:
SELECT * FROM approvals a
WHERE NOT EXISTS (
SELECT 1 FROM version v
WHERE v.idcontract = a.id_contract AND v.language = a.lang
AND v.idversion = a.version);
which resulted in 0 rows returned out of 701, ok my db should be good from the data pov.
So I checked the collation and for some reason the version table was latin1 vs utf8_generic_ci, ok so I restored the version table to utf8 with this.
ALTER TABLE `kidversion`
COLLATE = utf8_general_ci;
-- and for good measure
ALTER TABLE `kidversion`
CHANGE COLUMN `language` `language` VARCHAR(8)
CHARACTER SET 'utf8' COLLATE 'utf8_general_ci' NOT NULL;
But at this point I'm still getting an error while trying to create the foreign key:
ALTER TABLE `approvals`
ADD CONSTRAINT `approval_ibfk_1`
FOREIGN KEY (`version` , `lang` , `id_contract`)
REFERENCES `version` (`idversion` , `language` , `idcontract`)
ON DELETE NO ACTION
ON UPDATE NO ACTION;
And that results in a 1215 in every step I made. Surely I'm missing something.. can anyone help me out with this?
FOREIGN KEY (version , lang , id_contract) REFERENCES version (idversion , language , idcontract) AND PRIMARY KEY (idversion,idcontract,language) don't match fields have to be in the same order as well as same type.
'In the referencing table, there must be an index where the foreign key columns are listed as the first columns in the same order.' - https://dev.mysql.com/doc/refman/5.6/en/create-table-foreign-keys.html

MySql query error 150 puzzle

Im having to create and use a database for a class project. I developed the database and application locally using mysql workbench. It needs to be hosted on another service called myphpmyadmin. the problem is when I create my tables using the mysql execution line in that service I get the following error.
Error
SQL query:
CREATE TABLE IF NOT EXISTS `artists_residence` (
`artists_id` INT( 11 ) ,
`city_id` INT( 11 ) ,
PRIMARY KEY ( `artists_id` , `city_id` ) ,
CONSTRAINT FOREIGN KEY ( `artists_id` ) REFERENCES `artists` ( `id` ) ON DELETE CASCADE ON UPDATE CASCADE ,
CONSTRAINT FOREIGN KEY ( `city_id` ) REFERENCES `city` ( `id` ) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE = INNODB CHARSET = Latin1;
MySQL said: Documentation
#1005 - Can't create table 'fondellb-db.artists_residence' (errno: 150)
Here is the entire file of mySql i'm trying to execute.
CREATE TABLE IF NOT EXISTS `artists` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`fname` VARCHAR(255) NOT NULL,
`lname` VARCHAR(255) NOT NULL,
`year_of_birth` DATE NOT NULL,
`year_of_death` DATE NULL DEFAULT NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB CHARSET = Latin1;
CREATE TABLE IF NOT EXISTS `country` (
`country_id` INT(11) NOT NULL,
`name` VARCHAR(45) NOT NULL,
PRIMARY KEY (`country_id`))
ENGINE = InnoDB CHARSET = Latin1;
CREATE TABLE IF NOT EXISTS `city` (
`id` INT(11) NOT NULL,
`name` VARCHAR(255) NOT NULL,
`fk_country_id` INT(11),
PRIMARY KEY (`id`, `fk_country_id`),
CONSTRAINT `country_id`
FOREIGN KEY (`fk_country_id`)
REFERENCES `country` (`country_id`)
ON DELETE RESTRICT
ON UPDATE RESTRICT)
ENGINE = InnoDB CHARSET = Latin1;
CREATE TABLE IF NOT EXISTS `Medium` (
`id` INT(11) NOT NULL,
`name` VARCHAR(255) NOT NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB CHARSET = Latin1;
CREATE TABLE IF NOT EXISTS `artists_medium` (
`artists_id` INT(11),
`medium_id` INT(11),
PRIMARY KEY (`artists_id`, `medium_id`),
CONSTRAINT
FOREIGN KEY (`artists_id`)
REFERENCES `artists` (`id`)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT
FOREIGN KEY (`medium_id`)
REFERENCES `Medium` (`id`)
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB CHARSET = Latin1;
CREATE TABLE IF NOT EXISTS `artists_residence` (
`artists_id` INT(11),
`city_id` INT(11),
PRIMARY KEY (`artists_id`, `city_id`),
CONSTRAINT
FOREIGN KEY (`artists_id`)
REFERENCES `artists` (`id`)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT
FOREIGN KEY (`city_id`)
REFERENCES `city` (`id`)
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB CHARSET = Latin1;
I cant figure out what is failing in the final query. All the other queries are successful and the tables were created. Ive fiddled with that query in all sorts of variations and nothing seems to solve the error. Ive removed the explicit indexes, Ive changed the order of the constraints, checked all my type comparisons. This is a file that mysql exported based on the model i designed so I'm not sure why now i'm receiving this error. Please do not refer me to a reference to the error code or to another post that also receives this error code, I know what can cause the error but im wondering what is causing it here.
In this case - you're creating the following foreign key
CONSTRAINT FOREIGN KEY ( `city_id` ) REFERENCES `city` ( `id` ) ON DELETE CASCADE ON UPDATE CASCADE
while city table does not have an id primary key, but a composite primary key.
Obviously, the only one column of the 2 cannot guarantee uniqueness of the value hence mysql restricts you from creating a such FK.
So you must either change the city's PK or the artists_residence's FK.

MySQL foreign key ON DELETE SET NULL check data types error

I'm working on a normalised database, to be secure I wanted to use foreign keys.
My database:
CREATE TABLE `names` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(250) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `name` (`name`),
KEY `name_2` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
CREATE TABLE `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `name_id` (`name_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
The command:
ALTER TABLE `names` ADD FOREIGN KEY ( `name` ) REFERENCES `temp`.`users` (
`name_id`
) ON DELETE SET NULL ON UPDATE CASCADE ;
The response (error):
Error creating foreign key on name (check data types)
So, how to fix this?
The error is self explanatory. Name in names table is of type varchar(250) whereas name_id in users table is of type int(11).
But I believe you meant to have an FK all the way around in users table referencing names table.
ALTER TABLE users
ADD FOREIGN KEY (name_id) REFERENCES names (id)
ON DELETE SET NULL ON UPDATE CASCADE;
Here is SQLFiddle demo
Both keys have to be the same type and length,you have varchar and int.
Here is what you want:
ALTER TABLE `names` ADD FOREIGN KEY ( `id` ) REFERENCES `users` (
`name_id`)

Getting 1005 MySQL error, not sure why

I am trying to create database tables and get them connected. I have ensured that my data types are identical. I also ensure that my tables are made before added the FK. Any advice would be greatly appreciated.
SQL query:
ALTER TABLE `Member` ADD FOREIGN KEY ( memberId ) REFERENCES `Order_Head` ( `memberId` ) ;
MySQL said: Documentation
#1005 - Can't create table 'test2.#sql-184c_63' (errno: 150) (Details...)
-- ---
-- Table 'Order_Head'
--
-- ---
DROP TABLE IF EXISTS `Order_Head`;
CREATE TABLE `Order_Head` (
`orderId` INTEGER NOT NULL AUTO_INCREMENT,
`memberId` INTEGER NOT NULL,
`date` DATE NOT NULL,
PRIMARY KEY (`orderId`, `memberId`)
);
-- ---
-- Table 'Member'
--
-- ---
DROP TABLE IF EXISTS `Member`;
CREATE TABLE `Member` (
`memberId` INTEGER NOT NULL AUTO_INCREMENT,
`userName` VARCHAR(50) NOT NULL,
`password` VARCHAR(50) NOT NULL,
`email` VARCHAR(100) NOT NULL,
`isAdmin` BOOLEAN NOT NULL DEFAULT false,
PRIMARY KEY (`memberId`)
);
-- ---
-- Foreign Keys
-- ---
ALTER TABLE `Member` ADD FOREIGN KEY (memberId) REFERENCES `Order_Head` (`memberId`);
-- ---
-- Table Properties
-- ---
ALTER TABLE `Order_Head` ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
ALTER TABLE `Member` ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
It may be because the foreign key you are trying to set up is a primary key and has AUTO_INCREMENT, maybe what you meant was:
ALTER TABLE `Order_Head` ADD FOREIGN KEY (memberId) REFERENCES `Member` (`memberId`);
Any particular reason you're not creating the foreign keys inside the CREATE TABLE statement? If not, then you should maybe do so too.

How to update many-to-many related objects with Entity framework 1 & MySQL?

I have such a problem. I'm using EF1 with VS2008 SP1 & MySQL with MySQL Connector/Net 6.3.4
My database schema looks like this:
CREATE TABLE IF NOT EXISTS `credential` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;
CREATE TABLE IF NOT EXISTS `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;
CREATE TABLE IF NOT EXISTS `user_credential` (
`user_id` int(11) NOT NULL,
`credential_id` int(11) NOT NULL,
KEY `credential_id` (`credential_id`),
KEY `user_id` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
ALTER TABLE `user_credential`
ADD CONSTRAINT `user_credential_ibfk_2` FOREIGN KEY (`credential_id`) REFERENCES `credential` (`id`) ON DELETE CASCADE,
ADD CONSTRAINT `user_credential_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON DELETE CASCADE;
While I'm trying to execute the folowing code I have exception that I cannot understand
var entities = new studyEntities();
var user = new User { Name = "test" };
var credential = new Credential { Name = "admin" };
entities.AddToCredentialSet(credential);
entities.AddToUserSet(user);
entities.SaveChanges();
user.Credentials.Add(credential);
entities.SaveChanges(); // He I have a strange exception thrown
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(SELECT
`user_credential`.`credential_id`,
`user_credential`.`user_' at line 1
What does it mean? How can I see the whole query to seek the problem in it? or maybe what I'm doing wrong?
I had the same error because I had no primary key in the middle table.
This fixed it for me:
CREATE TABLE IF NOT EXISTS `user_credential` (
`user_id` int(11) NOT NULL,
`credential_id` int(11) NOT NULL,
PRIMARY KEY(`user_id`, `credential_id`)
KEY `user_id` (`user_id`)
CONSTRAINT .... FOREIGN KEY....
CONSTRAINT .... FOREIGN KEY....
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Looks like a bug in Connector /NET.
We have made a test, this code succeeded in dotConnect fro MySQL.
Add a primary key to the many-to-many table.
So if your table is
CREATE TABLE If Not Exists SubsidiaryMapping
(
CompanyId bigint NOT NULL,
SubsidiaryId bigint NOT NULL,
-- Add Primary Key composing exisiting keys
-- Following fixed issue for me
PRIMARY KEY(CompanyId , SubsidiaryId ),
CONSTRAINT .... FOREIGN KEY....
CONSTRAINT .... FOREIGN KEY....
);