I have tables as follows:
deliveries
- id (auto increment ID)
- supplier_name (string)
- delivery_date (datetime)
- ...
stock_movements
- id (auto increment ID)
- entity_type (string)
- entity_id (string) //This can hold a UUID or ID.
- qty (int)
- ...
Relationship
A delivery hasMany stock movements with deliveries : id being linked to stock_movements : entity_id
Problem
Calling:
$delivery->stockMovements()->delete();
Errors:
SQLSTATE[22007]: Invalid datetime format: 1292 Truncated incorrect DOUBLE value: '808498e7-a393-42f1-ab23-6ee89eb7040a' (SQL: delete from `stock_movements` where `stock_movements`.`entity_id` = 10000005 and `stock_movements`.`entity_id` is not null)
The raw SQL query is using the delivery ID as in integer but I think this fails as I need it to be a string, due to the cast type of the stock_movements entity_id.
Work Around
For now, I can get this to work by calling the delete in a loop.
$delivery->stockMovements()->each(function (StockMovement $sm){
$sm->delete();
});
Any ideas why doing it in a loop works fine while calling delete on all records at once fails?