mysql - NetBeans 中的 "cannot be added because it does not have a primary key"错误

标签 mysql jsp netbeans

我尝试从我的 MySQL 数据库创建一个实体类。 Netbeans 中的向导给我错误消息“无法添加,因为它没有主键”。但是我的表中有一个主键。好像是这个论坛别人遇到的bug:

https://netbeans.org/bugzilla/show_bug.cgi?id=167389

我已经尝试了线程中的建议,但没有得到它的工作。我错过了什么?我可以强制 Netbeans 导入实体类吗?

我正在使用 NetBeans 7.3

这是我的表格:

CREATE  TABLE IF NOT EXISTS `estelle`.`FrasVal` (
`ID` INT(11) NOT NULL AUTO_INCREMENT ,
`Varde` VARCHAR(45) CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NOT NULL ,
`Kommentar` VARCHAR(255) CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NULL ,
`RegistreratDatum` DATETIME NOT NULL ,
`FrasFragaSvarAlternativ_ID` INT(11) NULL ,
`Anvandare_ID` INT(11) NOT NULL ,
`Patient_ID` INT(11) NOT NULL ,
`FrasFraga_ID` INT(11) NOT NULL ,
PRIMARY KEY (`ID`) ,
UNIQUE INDEX `ID_UNIQUE` (`ID` ASC) ,
INDEX `fk_FrasVal_FrasFragaSvarAlternativ1_idx` (`FrasFragaSvarAlternativ_ID` ASC) ,
INDEX `fk_FrasVal_Anvandare1_idx` (`Anvandare_ID` ASC) ,
INDEX `fk_FrasVal_Patient1_idx` (`Patient_ID` ASC) ,
INDEX `fk_FrasVal_FrasFraga1_idx` (`FrasFraga_ID` ASC) ,
CONSTRAINT `fk_FrasVal_FrasFragaSvarAlternativ1`
FOREIGN KEY (`FrasFragaSvarAlternativ_ID` )
REFERENCES `estelle`.`FrasFragaSvarAlternativ` (`ID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_FrasVal_Anvandare1`
FOREIGN KEY (`Anvandare_ID` )
REFERENCES `estelle`.`Anvandare` (`ID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_FrasVal_Patient1`
FOREIGN KEY (`Patient_ID` )
REFERENCES `estelle`.`Patient` (`ID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_FrasVal_FrasFraga1`
FOREIGN KEY (`FrasFraga_ID` )
REFERENCES `estelle`.`FrasFraga` (`ID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_unicode_ci;

最佳答案

I have tried the suggestions in the thread but have not gotten it to work.

你确定吗?您发布的代码示例没有显示。如有必要,请更新您的问题以反射(reflect)问题的实际状态。


根据 the link you provided ,一个可能的(?)解决方法是拼写你的表格全部小写:

"Something that gave me a hard time is that for some funky reason the table names must be all in lower case. If tables names are in mixed case the relationships will not be discovered during the reverse engineering process. During my experimentation I discovered that the Middlegen docs give a warning about this, so I am guessing that the Eclipse DALI plugin uses Middlegen under the covers. The MySQL engine should also be INNODB."

https://netbeans.org/bugzilla/show_bug.cgi?id=167389#c11

The problem will happen if you have foreign keys where upper case and lower case table names don't match the referenced table's definition.


我建议 (1) 使用所有小写的表名和 (2) 确保表引用在外键约束上的拼写相同。

CREATE  TABLE IF NOT EXISTS `estelle`.`frasval` (
--                                     ^^^^^^^
 ...

REFERENCES `estelle`.`frasfragasvaralternativ` (`ID` )
--                    ^   ^    ^   ^

您应该也可以在您的表名中使用下划线 _(绝对让事情更具可读性!)。如果您有时间做一些实验,请发表您的结论!

关于mysql - NetBeans 中的 "cannot be added because it does not have a primary key"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18124529/

相关文章:

php - 密码验证无法将散列密码与用户输入的密码进行比较

java - 在ajax中调用java方法

java - 这个java项目需要多长时间才能完成

spring - 使用 Maven 构建 Spring 项目时,Netbeans 包括 XML 和属性

maven - 编译 nativetoascii maven 插件

netbeans - NetBeans 中的 "build"和 "build with dependencies"有什么区别?

MySQL 同时对同一个表执行 2 个查询

MySQL 查询按日期范围分组

php - 防止未经授权的 API 调用

java - 如何在jsp中将请求对象存储在 session 范围内