MySQLIntegrityConstraintException - "Unable to add or update child row: foreign key constraint failed" in Grails

I am trying to get order information from an XML file and try to save it in a database table (MySQL). I can get order information from an XML file, but when I try to insert into a table, it shows an error like MySQLIntegrityConstraintException.

My program contains, as shown below,

def Order = new XmlParser().parse("MyXml.xml")
def set1 = sql.dataSet("order_item")
def set2 = sql.dataSet("order_header")
Order.order_item.each {
// retrieving order information and storing
}
set1.add(Column_Name1:order_id,Column_Name2:field2,Column_Name3:field3)
set2.add(Column_Name1:order_id)

I described the error in detail below

WARNING: Failed to execute: insert into order_item (order_id, order_item_seq_id, order_item_type_id, product_id, prod_catalog_id, quantity, unit_price, unit_lis t_price, item_description, status_id) values ​​(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,? ,?) beca use: Unable to add or update the child row: foreign key failed ( ecommerc e/order_item , CONSTRAINT ORDER_ITEM_HDR FOREIGN KEY ( ORDER_ID ) LINKS order_header ( ORDER_ID )) Caught: com.mysql.jdbc.exceptionsIntbeciIntiSQLDec Cannot add or update a child row: foreign key constraint failed ( ecommerce /order_item , CONSTRAINT ORDER_ITEM_HDR FOREIGN KEY ( ORDER_ID ) LINKS o rder_header ( ORDER_ID )) com.mysql.jdbc.exceptions.jdbc4.MySQLInt egrityConstraintViolationException: Cann ot add or update the child row: foreign key failed ( ecommerce/order_i tem , CONSTRAINT ORDER_ITEM_HDR FOREIGN KEY ( ORDER_ID ) LINKS order_hea der ( ORDER_ID )) at com.mysql.jdbc.Uilil. at com.mysql.jdbc.Util.getInstance (Util.javahaps84) at com.mysql.jdbc.SQLError.createSQLException (SQLError.java:1041) at com.mysql.jdbc.MysqlIO.checkErrorPacket (MysqlIO ) at com.mysql.jdbc.MysqlIO.checkErrorPacket (MysqlIO.javahaps494) at com.mysql.jdbc.MysqlIO.sendCommand (MysqlIO.java:1960) at com.mysql.jdbc.MysqlIO.sqlQueryDirect 2114) at com.mysql.jdbc.ConnectionImpl.execSQL (ConnectionImpl.java:2696) at com.mysql.jdbc.PreparedStatement.executeInternal (PreparedStatement.ja in: 2105) at com.mysql.jdbc.PreparedStatement.executeUpdmen ( t.java: 2398) at com.mysql.jdbc.PreparedStatement.executeUpdate (PreparedStatement.java: 2316) at com.mysql.jdbc.PreparedStatement.executeUpdate (PreparedStatement.java: 2301) in db2xml.XMLTainDatabase $ .groovy: 52) in db2xml.XMLToDatabase.main (XMLToDatabase.groovy: 38)

Can someone help me out of this problem. thanks in advance

+4
source share
1 answer

First, you need to update the foreign key constraint table, after which you need to update a specific table. A slight change in the code above

set2.add(Column_Name1:order_id) set1.add(Column_Name1:order_id,Column_Name2:field2,Column_Name3:field3)

First you need to update set 2, and then set 1.

Now it worked for me. Thanks to everyone.

+3
source

All Articles