Search code examples
foreign-keysinnodbcreate-table

InnoDB error with create table


Have someone idea, why this SQL return error on create table accounts?

1005 - Can't create table 'mobilewaiter.accounts' (errno: 150)

I saw, that problem can be with index on keys, but everything refer on primary key. Help me, thanks.

CREATE TABLE IF NOT EXISTS `tables` (
  `_id` int(10) NOT NULL AUTO_INCREMENT,
  `tag` varchar(40) COLLATE utf8_czech_ci NOT NULL,
  `name` varchar(40) COLLATE utf8_czech_ci NOT NULL,
  `occupied` tinyint(1) NOT NULL DEFAULT '0',
  PRIMARY KEY (`_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_czech_ci AUTO_INCREMENT=1 ;

CREATE TABLE IF NOT EXISTS `users` (
  `_id` int(10) NOT NULL AUTO_INCREMENT,
  `nick` varchar(20) COLLATE utf8_czech_ci NOT NULL,
  `password` varchar(40) COLLATE utf8_czech_ci NOT NULL,
  `salt` varchar(20) COLLATE utf8_czech_ci NOT NULL,
  `email` varchar(50) COLLATE utf8_czech_ci NOT NULL,
  `firstname` varchar(20) COLLATE utf8_czech_ci NOT NULL,
  `lastname` varchar(20) COLLATE utf8_czech_ci NOT NULL,
  `role` varchar(20) COLLATE utf8_czech_ci NOT NULL,
  PRIMARY KEY (`_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_czech_ci AUTO_INCREMENT=1 ;

CREATE TABLE IF NOT EXISTS `accounts` (
  `_id` int(10) NOT NULL AUTO_INCREMENT,
  `table_id` varchar(10) COLLATE utf8_czech_ci NOT NULL,
  `state` varchar(20) COLLATE utf8_czech_ci NOT NULL DEFAULT 'created',
  `user_id` int(10) NOT NULL,
  `start_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `end_time` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`_id`),
  FOREIGN KEY (`table_id`) REFERENCES `tables`(`_id`) ON DELETE CASCADE ON UPDATE CASCADE,
  FOREIGN KEY (`user_id`) REFERENCES `users`(`_id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_czech_ci AUTO_INCREMENT=1 ;

Solution

  • table_id is varchar(10) but you want it to reference tables._id which is int. In order to use foreign keys the data types of the columns should be the same