java SQL 错误:违反完整性约束:外键没有父级
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32922870/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SQL Error : integrity constraint violation : foreign key no parent
提问by Yassin Hajaj
I'm struggling to find what I'm doing wrong with my query. I did not create the database but here is the structure :
我正在努力找出我的查询做错了什么。我没有创建数据库,但这是结构:
3 TABLES :
3张桌子:
vehicule: brand, motor, price, name, id
车辆:品牌、电机、价格、名称、ID
option: id, description, price
选项:ID、描述、价格
vehicule_option: id_vehicule, id_option
vehicule_option:id_vehicule,id_option
I guess that vehicule_option has 2 foreign keys but I can not find the problem with my addings. Here's my code which first part works fine :
我猜 vehicule_option 有 2 个外键,但我找不到我添加的问题。这是我的第一部分工作正常的代码:
public boolean create(Vehicule v){
String query = "INSERT INTO vehicule (MARQUE, MOTEUR, PRIX, NOM) VALUES (";
query += v.getMarque().getId() + ", "
+ v.getMoteur().getId() + ", "
+ v.getPrix() + ", \'"
+ v.getNom() + "\');";
for (Option o : v.getOptions()){
query += "INSERT INTO vehicule_option (id_vehicule, id_option) VALUES ("
+ v.getId() + ", " + o.getId() + ");";
}
try{
Statement state = this.connect.createStatement();
ResultSet rs = state.executeQuery(query);
} catch (SQLException e){
e.printStackTrace();
}
return true;
}
So, what I'm doing here is basically INSERT two values into the right columns. It gives me the following exception :
所以,我在这里所做的基本上是在正确的列中插入两个值。它给了我以下异常:
java.sql.SQLIntegrityConstraintViolationException: integrity constraint violation: foreign key no parent; SYS_FK_10132 table: VEHICULE_OPTION
at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
at org.hsqldb.jdbc.JDBCStatement.fetchResult(Unknown Source)
at org.hsqldb.jdbc.JDBCStatement.executeQuery(Unknown Source)
at fr.ocr.sql.DAOVehicule.create(DAOVehicule.java:35)
at fr.ocr.ihm.AddCarDialogBox.actionPerformed(AddCarDialogBox.java:151)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access0(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: org.hsqldb.HsqlException: integrity constraint violation: foreign key no parent; SYS_FK_10132 table: VEHICULE_OPTION
at org.hsqldb.error.Error.error(Unknown Source)
at org.hsqldb.Constraint.getException(Unknown Source)
at org.hsqldb.Constraint.checkInsert(Unknown Source)
at org.hsqldb.StatementDML.performIntegrityChecks(Unknown Source)
at org.hsqldb.StatementDML.insertSingleRow(Unknown Source)
at org.hsqldb.StatementInsert.getResult(Unknown Source)
at org.hsqldb.StatementDMQL.execute(Unknown Source)
at org.hsqldb.Session.executeCompiledStatement(Unknown Source)
at org.hsqldb.Session.executeDirectStatement(Unknown Source)
at org.hsqldb.Session.execute(Unknown Source)
... 40 more
回答by Ann Addicks
From what you have shown here, it looks like vehicle option has two foreign keys the id from the vehicle table and the id from the options table.
从您在此处显示的内容来看,车辆选项似乎有两个外键,分别是车辆表中的 id 和选项表中的 id。
When you do your insert either the vehicle id is not present in the vehicle table or the option id is not in its option table.
当您插入时,车辆 id 不在车辆表中,或者选项 id 不在其选项表中。
回答by Andreas
You just inserted a new vehicule
record, so how would you know the id of that record in v.getId()
?
您刚刚插入了一条新vehicule
记录,那么您如何知道该记录的 idv.getId()
呢?
You don't, and v.getId()
is likely 0
, since you're creating a new one.
你没有,而且v.getId()
很可能0
,因为你正在创建一个新的。
回答by AntonChe
In case you are iterating by "o" variable you should use o.getId() instead of v.getId()
如果您通过“o”变量进行迭代,您应该使用 o.getId() 而不是 v.getId()