Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Error : integrity constraint violation : foreign key no parent

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
like image 615
Yassin Hajaj Avatar asked Mar 05 '26 00:03

Yassin Hajaj


2 Answers

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.

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.

like image 172
Ann Addicks Avatar answered Mar 07 '26 14:03

Ann Addicks


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.

like image 43
Andreas Avatar answered Mar 07 '26 13:03

Andreas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!