1213: Deadlock found when trying to get lock; try restarting transaction
MySQLWhat does it mean?
InnoDB detected a deadlock in which two or more transactions wait for each other's locks and broke the cycle by rolling back one of the transactions.
Why does it happen?
Row locks acquired in different orders, cross updates of the same rows, wide row ranges being locked (gap locks) due to missing indexes, or conflicts during foreign key checks.
How to fix it
View the latest deadlock details: SHOW ENGINE INNODB STATUS (LATEST DETECTED DEADLOCK section). Access rows/tables in the same order in all transactions and keep transactions short. Narrow the locked range by adding proper indexes. Catch the rolled-back transaction in the application and retry it (retry logic).