I am having some issues with deleting data using Laravel 5. I seem to be stuck on a 'foreign key constraint', but I don't see why.
In my current database model, I have a datapoints table, which has a foreign key to the sensors table (datapoints.sensors_id -> sensor.id).
The code I am trying:
Route::get('/truncateData', function() {
DB::table('datapoints')->truncate();
DB::table('sensors')->truncate();
return 'Done...';
});
The result:
SQLSTATE[42000]: Syntax error or access violation: 1701 Cannot truncate a table referenced in a foreign key constraint (
alerting
.datapoints
, CONSTRAINTdatapoints_sensor_id_foreign
FOREIGN KEY (sensor_id
) REFERENCESalerting
.sensors
(id
)) (SQL: truncatesensors
)
I would understand this constraint if the order would be inverse (first deleting sensors), but when data points are empty, there should be no problem deleting sensors. I have also tried:
DB::table('datapoints')->delete();
DB::table('sensors')->delete();
return 'Done...';
Lastly, I also tried adding explicitly 'DB::commit()' between the delete statements, but all returned the same result.
Is this normal behavior? Am I missing something?