问题
So I have an error, when I want to add a foreign key. I added the foreign key in MySQL Workbench with an EER Diagram Model. The lines workbench tries to add are:`
CREATE SCHEMA IF NOT EXISTS `barberDB` DEFAULT CHARACTER SET latin1 ;
USE `barberDB` ;
-- -----------------------------------------------------
-- Table `barberDB`.`BARBER`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `barberDB`.`BARBER` (
`ID` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(45) NULL,
PRIMARY KEY (`ID`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `barberDB`.`CUSTOMER`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `barberDB`.`CUSTOMER` (
`ID` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(45) NULL,
`isHaircut` INT NULL,
`isBeard` INT NULL,
`isEyebrows` INT NULL,
`BARBER_ID` INT NOT NULL,
PRIMARY KEY (`ID`),
INDEX `fk_CUSTOMER_BARBER_idx` (`BARBER_ID` ASC) VISIBLE,
CONSTRAINT `fk_CUSTOMER_BARBER`
FOREIGN KEY (`BARBER_ID`)
REFERENCES `barberDB`.`BARBER` (`ID`)
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB;
SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
The error I get is:
ERROR: Error 1064: 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 'VISIBLE,
CONSTRAINT `fk_CUSTOMER_BARBER`
FOREIGN KEY (`BARBER_ID`)
REF' at line 12
SQL Code:
-- -----------------------------------------------------
-- Table `barberDB`.`CUSTOMER`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `barberDB`.`CUSTOMER` (
`ID` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(45) NULL,
`isHaircut` INT NULL,
`isBeard` INT NULL,
`isEyebrows` INT NULL,
`BARBER_ID` INT NOT NULL,
PRIMARY KEY (`ID`),
INDEX `fk_CUSTOMER_BARBER_idx` (`BARBER_ID` ASC) VISIBLE,
CONSTRAINT `fk_CUSTOMER_BARBER`
FOREIGN KEY (`BARBER_ID`)
REFERENCES `barberDB`.`BARBER` (`ID`)
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB
SQL script execution finished: statements: 6 succeeded, 1 failed
Fetching back view definitions in final form.
Nothing to fetch
I searched for solutions and find that parenthesis are important or backticks, but since the code is created by the tool, it seems correct to me. What could cause the error?
回答1:
For anyone looking this, in the EER Diagram Model window you can set wich MySQL version want use to generate SQL script. Go to Edit, Preferences, then Modeling section, MySQL and set your custom MySQL version.
回答2:
Check Your MySQL version and update it in:
Preferences -> Modeling -> MySQL -> Default Target MySQL Version
My MySQL version is 5.7.21.
回答3:
In MySQL Workbench, when you are connected to a server, click on "Server" from main menu and then "Server Status".
With "Version: X.X" go to your EER Diagram tag, click on "Edit" from main menu and "preferences", then in Modeling section "MySQL" set your custom MySQL version taked before.
回答4:
Thanks @imnothuman
In MacOS MySQL Workbench version, you can set the MySQL script version here:
From opened EER Diagram:
Model -> Model Options.
MySQL tab.
Uncheck "Use defaults from global settings".
Set version in "Target MySQL Version" input.
回答5:
i was using mysql version 5.5.62 .
commenting of the line
INDEX 'fk_CUSTOMER_BARBER_idx' ('BARBER_ID' ASC) VISIBLE,
did worked for me.
来源:https://stackoverflow.com/questions/51917812/mysql-error-1064-when-adding-foreign-key-with-mysql-workbench