44

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, CONSTRAINT datapoints_sensor_id_foreign FOREIGN KEY (sensor_id) REFERENCES alerting.sensors (id)) (SQL: truncate sensors)

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?

3 Answers 3

81

No, this is the way your database works. You can't truncate table that is referenced by some other table. You may do something like

DB::statement('SET FOREIGN_KEY_CHECKS=0;');
DB::table('datapoints')->truncate();
DB::table('sensors')->truncate();
DB::statement('SET FOREIGN_KEY_CHECKS=1;');

to disable foreign key checks, truncate tables and enable it again.

Like it was said above, for cross-database support Schema::disableForeignKeyConstraints() and Schema::enableForeignKeyConstraints() can be used to disable checks on foreign keys. The usage of Model::truncate() vs DB::table()->truncate() is just a matter of preference, although the former will always work even if table name of model will internally change, so I'd recommend it.

4
48

If you prefer to use Eloquent objects, Maksym's answer the "Eloquent" way

use Illuminate\Support\Facades\Schema;
use App\Models\Datapoint;
use App\Models\Sensor;


Schema::disableForeignKeyConstraints();
Datapoint::truncate();
Sensor::truncate();
Schema::enableForeignKeyConstraints();
0
13

In Laravel 7 and 8, for compatibility across 4 databases (MySql, Postgres, SQLite and SqlServer) and no Eloquent, you can use:

Schema::disableForeignKeyConstraints();
DB::table('datapoints')->truncate();
DB::table('sensors')->truncate();
Schema::enableForeignKeyConstraints();
3
  • I am also having the same issue, Am using Laravel 7. But this won't work for me. It throws an error as: Error Call to undefined method Illuminate\Database\MySqlConnection::schema(). How can I resolve this?
    – Zeenath
    Commented Aug 26, 2020 at 10:41
  • @Zeenath I have edited my answer. This should work.
    – Binar Web
    Commented Aug 28, 2020 at 10:32
  • Might have to do \Schema::
    – DeltaTango
    Commented Jan 30, 2021 at 15:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.