I have the YAML for the invoice data to go into the testing database:
After The Scope data for the website, the Customer Data, Product Data, and Order Data, there is the Invoice Data, and I will only paste that bit.
Everything works fine in terms of the data setup, up to inserting the invoice data, when i remove the Invoice Items Data. Once I add the Invoice Items Data, it gives me the following error in the SQL Execution, which cannot be right, as the data was valid when I exported it from MySQL to YAML.
Error:
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`xxxx_unit_tests`.`sales_flat_invoice_item`, CONSTRAINT `FK_SALES_FLAT_INVOICE_ITEM_PARENT` FOREIGN KEY (`parent_id`) REFERENCES `sales_flat_invoice` (`entity_id`) ON DELETE CASCADE ON UPDATE)
I checked the invoice data again, to ensure everything is correct, and saw that the second record was there, and not the first one, but there was no errors thrown when creating the invoice data, and thus the reason for the previous error, as there was no invoice with entity_id of 1.
I need to know why the second row was showing up, and not the first data set, with entity_id of 1 for invoices.
Has anyone else come across this issue? when I leave in just one invoice data set, and one invoice Item data set, then it works.
tables:
sales/invoice:
- entity_id: 1
store_id: 99
base_grand_total: 20.0000
shipping_tax_amount: 0.0000
tax_amount: 0.0000
base_tax_amount: 0.0000
store_to_order_rate: 1.0000
base_shipping_tax_amount: 0.0000
base_discount_amount: 0.0000
base_to_order_rate: 1.0000
grand_total: 20.0000
shipping_amount: 0.0000
subtotal_incl_tax: 20.0000
base_subtotal_incl_tax: 20.0000
store_to_base_rate: 1.0000
base_shipping_amount: 0.0000
total_qty: 1.0000
base_to_global_rate: 1.0000
subtotal: 20.0000
base_subtotal: 20.0000
discount_amount: 0.0000
billing_address_id: 1
order_id: 1
state: 1
store_currency_code: "USD"
order_currency_code: "USD"
base_currency_code: "USD"
global_currency_code: "USD"
increment_id: 200009925
created_at: "2012-01-19 23:40:27"
updated_at: "2012-01-19 23:40:27"
hidden_tax_amount: 0.0000
base_hidden_tax_amount: 0.0000
- entity_id: 2
store_id: 99
base_grand_total: 20.0000
shipping_tax_amount: 0.0000
tax_amount: 0.0000
base_tax_amount: 0.0000
store_to_order_rate: 1.0000
base_shipping_tax_amount: 0.0000
base_discount_amount: 0.0000
base_to_order_rate: 1.0000
grand_total: 20.0000
shipping_amount: 0.0000
subtotal_incl_tax: 20.0000
base_subtotal_incl_tax: 20.0000
store_to_base_rate: 1.0000
base_shipping_amount: 0.0000
total_qty: 1.0000
base_to_global_rate: 1.0000
subtotal: 20.0000
base_subtotal: 20.0000
discount_amount: 0.0000
billing_address_id: 1
order_id: 2
state: 2
store_currency_code: "USD"
order_currency_code: "USD"
base_currency_code: "USD"
global_currency_code: "USD"
increment_id: 200009925
created_at: "2012-01-19 23:40:27"
updated_at: "2012-01-19 23:40:27"
hidden_tax_amount: 0.0000
base_hidden_tax_amount: 0.0000
sales/invoice_item:
- entity_id: 1
parent_id: 1
base_price: 20.0000
base_weee_tax_row_disposition: 0.0000
weee_tax_applied_row_amount: 0.0000
base_weee_tax_applied_amount: 0.0000
base_row_total: 20.0000
row_total: 20.0000
weee_tax_row_disposition: 0.0000
base_weee_tax_disposition: 0.0000
price_incl_tax: 20.0000
weee_tax_applied_amount: 0.0000
base_price_incl_tax: 20.0000
qty: 1.0000
weee_tax_disposition: 0.0000
base_weee_tax_applied_row_amount: 0.0000
price: 20.0000
base_row_total_incl_tax: 20.0000
row_total_incl_tax: 20.0000
product_id: 1
order_item_id: 1
weee_tax_applied: "a:0:{}"
sku: "gift"
name: "Test Giftcard"
- entity_id: 2
parent_id: 2
base_price: 20.0000
base_weee_tax_row_disposition: 0.0000
weee_tax_applied_row_amount: 0.0000
base_weee_tax_applied_amount: 0.0000
base_row_total: 20.0000
row_total: 20.0000
weee_tax_row_disposition: 0.0000
base_weee_tax_disposition: 0.0000
price_incl_tax: 20.0000
weee_tax_applied_amount: 0.0000
base_price_incl_tax: 20.0000
qty: 1.0000
weee_tax_disposition: 0.0000
base_weee_tax_applied_row_amount: 0.0000
price: 20.0000
base_row_total_incl_tax: 20.0000
row_total_incl_tax: 20.0000
product_id: 1
order_item_id: 2
weee_tax_applied: "a:0:{}"
sku: "gift"
name: "Test Giftcard"
I have run out of ideas, as to what is causing this issue
Found out what the issue was.
It was caused by one thing, but the error indicated something else. A nasty gotcha.
The problem child was the second invoice, having the same increment_id.
When the data gets added tot he test database, it does an insert/update_on_duplicate statement, which did something completely weird.
The first invoice got inserted without any issues, and when it came to the point of inserting the second invoice, it realized the increment_id field, which has a Unique index on it, is the same as on the first invoice, and then used the data from the second invoice, and updated the first inserted record with that data.
The second invoice had an entity_id of “2”, so that then became the entity_id of the first inserted record, and made it look like only the second invoice data was inserted, and not the first invoice data.
When it got to the point where the Invoice Items was being inserted, the “Integrity constraint violation” error got flagged behind the scenes, where I traced it down to, as there was no Invoice with entity_id of “1” in the database, to link the child item to, as its parent’s entity_id was updated to “2”.
Maybe the “Update on duplicate” part of the insert, should be switched off, when you know that you are not going to update data, in the test.