Mysql Workbench Can't Select Foreign Key

依然范特西╮ 提交于 2021-01-27 10:17:10

问题


First of all I've examined a lot of questions and googled it a lot, none of them solved my problem..

I'm creating my tables by using Mysql Workbench 6.3. I've created them by using only gui not a single query. After that I've tried to create some foreign keys for int(11) columns but GUI is not allowing me to do so.

Here's my Show Create Table 'tableName'

CREATE TABLE `item` (
  `id` int(11) NOT NULL,
  `description` varchar(300) NOT NULL,
  `maker` varchar(200) NOT NULL,
  `model` varchar(200) NOT NULL,
  `condition` varchar(200) NOT NULL,
  `locationId` int(11) NOT NULL,
  `categoryId` int(11) NOT NULL,
  `userId` int(11) NOT NULL,
  `lendTermsId` int(11) NOT NULL,
  `itemOptionsId` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `id_UNIQUE` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

And the other one ;

CREATE TABLE `location` (
  `id` int(11) NOT NULL,
  `type` varchar(200) NOT NULL,
  `coordinateLat` varchar(200) NOT NULL,
  `coordinateLong` varchar(45) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `id_UNIQUE` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

I'm trying to create a foreign key between location.id and item.locationId. Workbench GUI is not allowing me to select checkboxes...

As you can see from this image I can't select checkbox or select combobox value..

EDIT: I can manage this with query, but there are a lot of tables in some projects, dont want to do it all with queries... Is there something that I'm missing in workbench ?

Thanks in advance.


回答1:


As user4115463 said, this is caused by a difference in DataType. Make sure that both keys have the exact same data type:

  • the Datatype field should contain the exact same string (INT and INT(10) are not the same)
  • the B (Is binary) checkbox should match
  • the UN (Unsigned) checkbox should match

When selecting a checkbox, MySQL Workbench attempts to populate the dropdown next to the checkbox with matching columns, and if it can't find any matching columns, it decides to silently ignore the click on the checkbox instead.




回答2:


It might be a little late to answer this question but, Recently, I experience this issue in Workbench GUI and it was caused by difference in DataType.

Making sure that both fields have the same DataType seems to resolve the issue.

Hope this helps.




回答3:


Try the following Alt statement

 ALTER TABLE `emp`.`item` 
ADD INDEX `location_idx` (`categoryId` ASC);
ALTER TABLE `emp`.`item` 
ADD CONSTRAINT `location`
  FOREIGN KEY (`categoryId`)
  REFERENCES `emp`.`location` (`id`)
  ON DELETE NO ACTION
  ON UPDATE NO ACTION;

I have tested through GUI check this screenshot



来源:https://stackoverflow.com/questions/32283995/mysql-workbench-cant-select-foreign-key

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!