Mysql – prevent MySQL Workbench to add index for foreignkey constraint when it is first part of primary

constraintforeign keyMySQLmysql-workbenchprimary-key

I know for each foreign key we should make an index on its column, But it is not necessary when that column in first part of PRIMARY index.

MySQL accepts this query:

CREATE TABLE IF NOT EXISTS `poll_option_i18ns` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `poll_option_id` int(11) NOT NULL,
  `lang` char(2) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
  `title` varchar(127) COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`poll_option_id`,`lang`),
  KEY `id` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;

ALTER TABLE `poll_option_i18ns`
  ADD CONSTRAINT `poll_option_id` FOREIGN KEY (`poll_option_id`) REFERENCES `poll_options` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION;

But in MySQL workbench I couldn't add foreignkey without index!
It is MySQL Workbench bug or I do it wrong way?

Best Answer

That's currently a limitation of MySQL Workbench and has been mentioned recently already on Stackoverflow (can't find the question right now, sorry). MySQL Workbench doesn't use the special case where a foreign key uses columns that start the PK in that table. Please file a feature request at http://bugs.mysql.com if you think it would be worth implementing it.