SQL错误:完整性约束违规:外键没有父项 [英] SQL Error : integrity constraint violation : foreign key no parent

查看:205
本文介绍了SQL错误:完整性约束违规:外键没有父项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力寻找我做错了我的查询。
我没有创建数据库,但是这里是结构:

3 TABLES:

强力>车辆:品牌,马达,价格,名称,ID

选项:id,description,price



vehicule_option :id_vehicule,id_option

我猜vehicleclect_option有2个外键,找到我添加的问题。
这里是我的代码,其中第一部分工作正常:

$ p $ public boolean create(Vehicule v){
String (;
query + = v.getMarque()。getId()+,
+ v.getMoteur()中查询=INSERT INTO vehicule(MARQUE,MOTEUR,PRIX,NOM)VALUES getId()+,
+ v.getPrix()+,\'
+ v.getNom()+\');;

for(Option o:v.getOptions()){
query + =INSERT INTO vehicule_option(id_vehicule,id_option)VALUES(
+ v.getId()+ ,+ o.getId()+);;
}


尝试{
Statement state = this.connect.createStatement();
ResultSet rs = state.executeQuery(query);
} catch(SQLException e){
e.printStackTrace();
}
返回true;





$ b因此,我在这里所做的基本上是将两个值插入右边列。
它给了我以下例外:

pre $ java.sql.SQLIntegrityConstraintViolationException:完整性约束冲突:外键没有父项; SYS_FK_10132表:VEHICULE_OPTION
在org.hsqldb.jdbc.JDBCUtil.sqlException(未知源)
在org.hsqldb.jdbc.JDBCUtil.sqlException(未知源)
在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 $ 1.actionPerformed(AddCarDialogBox.java:151)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton $ Handler.actionPerformed(未知源)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener。 mouseReleased(Unknown Source)
在java.awt.Component.processMouseEvent(未知源)$ b $在javax.swing.JComponent.processMouseEvent(未知源)
在java.awt.Co mponent.processEvent(未知源)
在java.awt.Container.processEvent(未知源)
在java.awt.Component.dispatchEventImpl(未知源)
在java.awt.Container。 dispatchEventImpl(Unknown Source)
在java.awt.Component.dispatchEvent(未知源)
在java.awt.LightweightDispatcher.retargetMouseEvent(未知源)
在java.awt.LightweightDispatcher.processMouseEvent未知源)
在java.awt.LightweightDispatcher.dispatchEvent(未知源)
在java.awt.Container.dispatchEventImpl(未知源)
在java.awt.Window.dispatchEventImpl(未知源)
在java.awt.Component.dispatchEvent(未知源)
在java.awt.EventQueue.dispatchEventImpl(未知源)
在java.awt.EventQueue.access $ 500(未知源)
在java.awt.EventQueue $ 3.run(未知源)$ b $在java.awt.EventQueue $ 3.run(未知源)$ b $在java.security.AccessController.doPrivileged(原生方法)$ java.util.ProtectionDomain上的
$ 1.doIntersectionPrivilege(未知源)$ java.util.ProtectionDomain上的b $ b $ 1.doIntersectionPrivilege(未知源)
上的java.awt.EventQueue $ 4.run(未知源)
在java.awt.EventQueue $ 4.run(未知源)
在java.security.AccessController.doPrivileged(本地方法)$ b $在java.security.ProtectionDomain $ 1.doIntersectionPrivilege(未知源)
在java.awt.EventQueue.dispatchEvent(未知源)
在java.awt.EventDispatchThread.pumpOneEventForFilters(未知源)
在java.awt.EventDispatchThread.pumpEventsForFilter(未知源)
在java.awt.EventDispatchThread.pumpEventsForHierarchy(未知源)
在java.awt.EventDispatchThread.pumpEvents(未知源)
在java.awt.EventDispatchThread.pumpEvents(未知源)
在java.awt.EventDispatchThread.run(未知源)
导致:org.hsqldb.HsqlException:完整性约束违规:外键没有父母; SYS_FK_10132表:VEHICULE_OPTION
在org.hsqldb.error.Error.error(未知源)
在org.hsqldb.Constraint.getException(未知源)
在org.hsqldb.Constraint.checkInsert (未知来源)
在org.hsqldb.StatementDML.performIntegrityChecks(未知来源)
在org.hsqldb.StatementDML.insertSingleRow(未知来源)
在org.hsqldb.StatementInsert.getResult(未知源)
在org.hsqldb.StatementDMQL.execute(未知源)
在org.hsqldb.Session.executeCompiledStatement(未知源)
在org.hsqldb.Session.executeDirectStatement(未知源)
at org.hsqldb.Session.execute(Unknown Source)
... 40 more


车辆记录,那么如何知道<$ c $中记录的id c> v.getId()?



您不要, v.getId()可能是 0 ,因为你是cre在新的一个。


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 :

vehicule : brand, motor, price, name, id

option : id, description, price

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 :

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$1.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.access$500(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.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

解决方案

You just inserted a new vehicule record, so how would you know the id of that record in v.getId()?

You don't, and v.getId() is likely 0, since you're creating a new one.

这篇关于SQL错误:完整性约束违规:外键没有父项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆