Sabarish
Sabarish

Reputation: 1184

MySQLIntegrityConstraintException - “Cannot add or update a child row: a foreign key constraint fails” in Grails

Am trying to get some order information from XML file and trying to store it in database(MySQL) table. I can retrieve the order information from XML file but when am trying to insert into the table it shows error as MySQLIntegrityConstraintException.

My program contains as 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 listed 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: Cannot add or update a child row: a foreign key constraint fails (ecommerc e/order_item, CONSTRAINT ORDER_ITEM_HDR FOREIGN KEY (ORDER_ID) REFERENCES order_header (ORDER_ID)) Caught: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationExcepti on: Cannot add or update a child row: a foreign key constraint fails (ecommerce /order_item, CONSTRAINT ORDER_ITEM_HDR FOREIGN KEY (ORDER_ID) REFERENCES o rder_header (ORDER_ID)) com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cann ot add or update a child row: a foreign key constraint fails (ecommerce/order_i tem, CONSTRAINT ORDER_ITEM_HDR FOREIGN KEY (ORDER_ID) REFERENCES order_hea der (ORDER_ID)) at com.mysql.jdbc.Util.handleNewInstance(Util.java:409) at com.mysql.jdbc.Util.getInstance(Util.java:384) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1041) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3562) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3494) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1960) at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2114) at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2696) at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.ja va:2105) at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java :2398) at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java :2316) at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java :2301) at db2xml.XMLToDatabase$_main_closure2.doCall(XMLToDatabase.groovy:52) at db2xml.XMLToDatabase.main(XMLToDatabase.groovy:38)

Can anyone help me to come out from this problem. Thanks in advance

Upvotes: 4

Views: 8276

Answers (1)

Sabarish
Sabarish

Reputation: 1184

At first the foreign key constraint table to be updated initially then the specific table to should me updated. A small change in the above code is

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

The set 2 should updated first and then the set 1.

Now it got worked for me. Thanks for all.

Upvotes: 3

Related Questions