Dale
Dale

Reputation: 93

Database import situation

Mysql doesn't want to add this database into my localhost database section.

Am I doing something wrong?

db.sql This tutorial: https://github.com/samanz/cakecart

Error:

SQL query:

CREATE TABLE `categories` (
`id` INT( 11 ) NOT NULL AUTO_INCREMENT ,
`name` VARCHAR( 50 ) NULL default NULL ,
`parent_id` INT( 11 ) UNSIGNED default '0',
`order` INT( 3 ) default '0',
`image` VARCHAR( 50 ) NULL default NULL ,
`ids` VARCHAR( 225 ) NULL default NULL ,
`url` VARCHAR( 255 ) NULL default NULL ,
PRIMARY KEY ( `id` ) ,
FOREIGN KEY ( `parent_id` ) REFERENCES categories( `id` ) ,
UNIQUE KEY `url` ( `url` )
);

MySQL said: Documentation
#1005 - Can't create table 'cake_cart.categories' (errno: 150) 

Upvotes: 0

Views: 117

Answers (2)

Brian
Brian

Reputation: 3023

Error 150 is a foreign key problem. Likely caused by:

FOREIGN KEY ( `parent_id` ) REFERENCES categories( `id` ) ,

You can't make a "foreign" key reference to the same table you're creating. Simply make the parent_id column indexed instead.

KEY `parent_id` ( `parent_id` ) ,

Upvotes: 2

rackemup420
rackemup420

Reputation: 1567

Should look something like this...

CREATE TABLE IF NOT EXISTS `categories` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(50) NOT NULL,
  `parent_id` int(11) unsigned NOT NULL DEFAULT '0',
  `order` int(3) NOT NULL DEFAULT '0',
  `img` varchar(50) NOT NULL,
  `ids` varchar(255) NOT NULL,
  `url` varchar(255) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `url` (`url`),
  KEY `parent_id` (`parent_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

I updated the structure and ran it on my database and it worked.

Upvotes: 1

Related Questions