SQL错误:完整性约束违规:外键没有父

时间:2015-10-03 12:59:06

标签: java sql foreign-keys

我很难找到我的查询错误。 我没有创建数据库,但这里是结构:

3 TABLES:

vehicule :品牌,电机,价格,名称,ID

选项:ID,说明,价格

vehicule_option :id_vehicule,id_option

我想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;
    }

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

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

3 个答案:

答案 0 :(得分:3)

从你在这里显示的内容看,车辆选项有两个外键,来自车辆表的id和来自选项表的id。

当您进行插入时,车辆表中不存在车辆ID或选项ID不在其选项表中。

答案 1 :(得分:2)

您刚刚插入了新的vehicule记录,那么您如何知道v.getId()中该记录的ID?

您不会,v.getId()可能0,因为您正在创建一个新的。{/ p>

答案 2 :(得分:0)

如果要通过“ o”变量进行迭代,则应使用o.getId()而不是v.getId()