使用EcomDev模块进行Magento PHPUnit测试时,在插入发票项时会出现SQL错误

时间:2012-01-20 07:17:34

标签: magento phpunit

我有发票数据的YAML进入测试数据库:

在网站的范围数据,客户数据,产品数据和订单数据之后,有发票数据,我只会粘贴该位。

当我删除发票项目数据时,在数据设置方面一切正常,直到插入发票数据。一旦我添加了发票项数据,它就会在SQL执行中出现以下错误,这可能是正确的,因为当我将它从MySQL导出到YAML时数据是有效的。

错误:

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)

我再次检查发票数据,以确保一切正确,并看到第二条记录在那里,而不是第一条记录,但是在创建发票数据时没有抛出错误,因此之前的原因错误,因为没有发票,其entity_id为1。

我需要知道为什么第二行显示,而不是第一个数据集,发票的entity_id为1。

还有其他人遇到过这个问题吗?当我只留下一个发票数据集和一个发票项目数据集时,它就可以工作。

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"

关于造成这个问题的原因,我已经没有想法了

1 个答案:

答案 0 :(得分:4)

找出问题所在。

这是由一件事引起的,但错误表明了别的东西。一个讨厌的问题。

问题孩子是第二张发票,具有相同的increment_id。 当数据被添加到测试数据库中时,它会执行insert / update_on_duplicate语句,这会做一些非常奇怪的事情。

try {
    $this->_getWriteAdapter()->insertOnDuplicate(
         $this->getTable($tableEntity),
         $records
    );
} catch (Exception $e) {
    throw new EcomDev_PHPUnit_Model_Mysql4_Fixture_Exception(
        sprintf('Unable to insert/update records for a table "%s"', $tableEntity), 
        $e
    );
}

插入第一张发票没有任何问题,当插入第二张发票时,它意识到increment_id字段,其上有唯一索引,与第一张发票上的相同,然后使用第二张发票中的数据,并使用该数据更新第一个插入的记录。

第二张发票的entity_id为“2”,因此它成为第一张插入记录的entity_id,并使其看起来只插入了第二张发票数据,而不是第一张发票数据。

当它到达插入发票项目的位置时,“完整性约束违规”错误在幕后被标记,我将其追溯到,因为没有发票,其中entity_id为“1”数据库,用于将子项链接到,因为其父项的entity_id已更新为“2”。

在测试中,当您知道不打算更新数据时,可能会关闭插入的“重复更新”部分。