【问题标题】:SQL Error : integrity constraint violation : foreign key no parentSQL 错误:违反完整性约束:外键没有父级
【发布时间】:2015-10-03 12:59:05
【问题描述】:

我正在努力找出我的查询做错了什么。 我没有创建数据库,但结构如下:

3 个表格:

车辆:品牌、电机、价格、名称、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

【问题讨论】:

  • 你打过v.setId吗?如果不是,则不太可能与数据库中的 id 具有相同的值。
  • 我刚起鸡皮疙瘩。又一次邀请您通过SQL Injection 攻击丢失所有数据。
  • 我认为是 v.getId() 出了问题。当数据库中的 ID 必须类似于 20 时,它给出 0
  • @Andreas LOL,因为它是一个离线 SWING 应用程序,我不担心 :)

标签: java sql foreign-keys


【解决方案1】:

从您在此处显示的内容来看,车辆选项似乎有两个外键,即来自车辆表的 id 和来自选项表的 id。

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

【讨论】:

    【解决方案2】:

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

    你没有,v.getId() 很可能是0,因为你正在创建一个新的。

    【讨论】:

    • 这确实是对的。现在我面临一个问题。你能帮我解决这个问题吗?我也不明白。我正在编辑我的问题。
    • @YassinHajaj 在您已经有两个答案后,请不要更改问题。如果您有新问题,请提出新问题。否则,你会嘲笑你已经得到的好答案。
    • @DavidWallace 好的,我正在提出一个新问题。
    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 2014-03-17
      • 1970-01-01
      • 2016-04-24
      • 1970-01-01
      • 2014-11-09
      • 1970-01-01
      • 2021-02-17
      • 2020-07-19
      • 2019-10-03
      相关资源
      最近更新 更多