I seem to have many field references to line items that do not exist. This query returns 147 records:
SELECT *
FROM field_data_commerce_line_items
WHERE
commerce_line_items_line_item_id NOT IN (SELECT line_item_id FROM commerce_line_item)
I don't know how this happened, I can't reproduce it. This is causing me a problem because I am trying to use the Commerce Cart Expire module to remove old orders (my database is getting huge), but when the module tries to delete an order which contains a reference to a line item that does not exist it silently fails.
At this point I don't have enough information to open an issue on why this is happening and even if I did, the issue is probably with Entity API or Entity Reference modules. I am looking for some advice:
- How can I safely delete these fields?
- Any ideas on how to get to the bottom of this?
Thanks!
Comments
Comment #1
rszrama commentedI'm not sure if there's any way to tell how those references weren't cleaned up in the first place. We'd need to know at least the type of line item they were referencing to sort that out. At a core level, when a line item is deleted, we first delete any references to it via commerce_line_item_delete_references(). This must happen before the line item is deleted, so perhaps those line items were deleted outside of normal API usage.
To clean up those references, I'm not sure what you can do. For the time being, you might just need to patch the other module to check to ensure the line item exists before attempting to traverse to it via the entity metadata wrapper or something. You should, however, be able to safely delete those rows from your field data tables so long as you also delete the related rows from the field data revision tables and clear the field cache when done.
Comment #2
bander2 commentedThanks rszrama. I'll take your advice.