SQLAlchemy:当值为s时,merge为什么会发出一些列为None的更新

2024-09-30 12:23:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图通过创建一个新实例,设置id,然后合并来更新DB行

    inbound_shipment = self.factory.build_from_mws(
        account.id, amazon_account.id, inbound_shipment_data, inbound_shipment_items_data,
    )
    if existing:
        inbound_shipment.id = existing.id
        for item in inbound_shipment.inbound_shipment_items:
            # setting the inbound shipment id just in case
            item.inbound_shipment_id = inbound_shipment.id
    session = FBAInboundShipment.get_session()
    merged = session.merge(inbound_shipment)
    session.commit()

工厂创建一个模型(及其子对象)实例而不保存它,也不分配id。您可以看到FBAInboundShipmentItem.account_id是在构造时设置的:

    def build(self, account_id, amazon_account_id, inbound_shipment_data, inbound_shipment_items_data):
        inbound_shipment_items = []
        for item_data in inbound_shipment_items_data:
            prep_details_data = item_data.pop("prep_details_list", [])
            prep_details = [FBAPrepDetails(**pd) for pd in prep_details_data]
            inbound_shipment_items.append(
                FBAInboundShipmentItem(account_id=account_id, prep_details_list=prep_details, **item_data)
            )

        inbound_shipment = FBAInboundShipment(
            account_id=account_id,
            amazon_account_id=amazon_account_id,
            inbound_shipment_items=inbound_shipment_items,
            **inbound_shipment_data
        )

        return inbound_shipment

合并进行得很好,但是当我提交时,我得到一个带有account\ id的查询,当然失败了,因为它有一个FK约束

下面是这部分过程中的一些调试

这是在合并之前,发货中的最后一项,在我设置发货id的上面的循环中。您可以看到account_id有一个值:

ipdb> p item
<FBAInboundShipmentItem created_at:None, id:None, line_item_id:None, account_id:5, inbound_shipment_id:48, shipment_id:FBA85628, seller_sku:7195224668716, fulfillment_network_sku:0002632535172, quantity_shipped:12, quantity_received:18, quantity_in_case:27, release_date:2019-11-06 00:00:00>

这是在合并之后,这里仍然有一个值

ipdb> p merged.inbound_shipment_items[0]
<FBAInboundShipmentItem created_at:None, id:None, line_item_id:None, account_id:5, inbound_shipment_id:48, shipment_id:FBA85628, seller_sku:1974002868496, fulfillment_network_sku:6341934464365, quantity_shipped:5, quantity_received:5, quantity_in_case:38, release_date:2019-11-02 00:00:00>

但是当.commit()被调用时:

IntegrityError: (_mysql_exceptions.IntegrityError) (1452, 'Cannot add or update a child row: a foreign key constraint fails (`mydb`.`rst_fba_inbound_shipment_items`, CONSTRAINT `rst_fba_inbound_shipment_items_ibfk_2` FOREIGN KEY (`account_id`) REFERENCES `accounts` (`id`) ON DELETE CASCADE)')
[SQL: UPDATE rst_fba_inbound_shipment_items SET account_id=%s, inbound_shipment_id=%s WHERE rst_fba_inbound_shipment_items.id = %s]
[parameters: ((None, None, 1256L), (None, None, 1257L), (None, None, 1258L), (None, None, 1259L), (None, None, 1260L), (None, None, 1261L), (None, None, 1262L), (None, None, 1263L)  ... displaying 10 of 28 total bound parameter sets ...  (None, None, 1282L), (None, None, 1283L))]

Tags: innoneidamazondatasessionitemsaccount

热门问题