Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Truncate tables with foreign key constraints in a Laravel seed file.

For the scenario, imagine posts has a foreign key user_id referencing

public function up()
	Schema::create('posts', function(Blueprint $table) {

running seeds with truncate() will trigger a constraint violation if foreign key checks is not disabled.

	Syntax error or access violation: 1701 Cannot truncate a table referenced in a foreign key constraint ...

The fix:


class DatabaseSeeder extends Seeder {

	 * Run the database seeds.
	 * @return void
	public function run()

		//disable foreign key check for this connection before running seeders
		DB::statement('SET FOREIGN_KEY_CHECKS=0;');

		// supposed to only apply to a single connection and reset it's self
		// but I like to explicitly undo what I've done for clarity
		DB::statement('SET FOREIGN_KEY_CHECKS=1;');


fix from here

As pointed out in the comments at SO you wouldn't want to disable foreign key checks on a production app but you really don't want to run seed files in production either so obviously this is not intended for production but helpful in development.

Copy link

igorbabko commented May 21, 2016

Great! Thanks.

Copy link

Srivani17 commented Jun 21, 2016

Tank you

Copy link

LeonSkrilec commented Jun 28, 2016

Thanks for that!

Copy link

natalia-stovbun commented Jul 7, 2016

It works! Thanks!

Copy link

kriskornel commented Jul 25, 2016

Yup. It works for me too

Copy link

IProSoft commented Aug 18, 2016

Thanks for tip!

Copy link

achintharodrigo commented Oct 9, 2016


Copy link

j3rrey commented Nov 14, 2016


Copy link

mikedamoiseau commented Jan 24, 2017

This code seems to work too (MySQL, Laravel 5.2.45):


Copy link

umutyerebakmaz commented Mar 4, 2017

Good experience thank u

Copy link

zachu90 commented May 7, 2017

Thanks this worked for me!

Copy link

hatamiarash7 commented May 27, 2017

Awesome :) i have many problems in last 30 minute 👍 💯

Copy link

blues911 commented Jun 20, 2017

Tested in Laravel4, Thanks a lot)

Copy link

ggwebdev commented Jul 6, 2017

Works great now! Tanks

Copy link

SOSTheBlack commented Nov 26, 2017

Muito Obrigado!

Copy link

tahiryasin commented Jan 30, 2018

@mikedamoiseau You are the man ! 👍 💯

Copy link

ogigpermana commented May 4, 2018

@mikedamoiseau Thanks sir this is works great!

Copy link

serdarsen commented May 21, 2018

Thank you!

Copy link

ezequiel9 commented Aug 2, 2018

Great man!

Copy link

micessien commented Nov 16, 2018

Thank you!

Copy link

yunusemremert commented Mar 5, 2019

thank you bro!

Copy link

M-Abdullahi commented Apr 16, 2019

Another option would be to use the schema facade to disable the fk constraint.

* Reverse the migrations.
* @return void
public function down()

I was also thinking on the idea of creating 2 separate migrations; 
one to disable and another one to enable the fk constraints. 

You can edit the timestamp naming part and place the fk enabling migration to 
run as the _first_ migration and the disabling one to run _last_ after  all other migrations have run. 

~  a thought that i haven't tried, yet.

FYI: I use mysql and do this on development environment 

Copy link

MutlaqAldhbuiub commented Dec 17, 2019

It works for me!

Copy link

yunuselci commented Jan 14, 2020

thx! works for me too

Copy link

Amvos2000 commented Jan 17, 2020

Thx this saved my project

Copy link

RyotaBannai commented May 15, 2020

@mikedamoiseau thank you!!

Copy link

edrisaturay commented May 23, 2020

Thanks for this fix. works on laravel 7

Copy link

pishguy commented May 29, 2020

you can use this code on Laravel 7




Copy link

henryavila commented Sep 4, 2020

Hi, I have this problem in MsSQL, and I conclude that I have just 2 options (beside moving away from MSSQL =D)

  1. Remove the foreign key, truncate and recreate the foreign key
  2. Delete table data and reset the Auto increment

I Found the approach 2 much better. Here is the working code:

$tableName = (new Model())->getTable();
\Illuminate\Support\Facades\DB::table($tableName )->delete();
\Illuminate\Support\Facades\DB::statement("DBCC CHECKIDENT ({$tableName }, RESEED, 0)");

Baiscally its what the truncate does (obviously the truncate is much faster), But it work.

Copy link

remusbejusca87 commented Mar 31, 2022

thanks man, it worked for me!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment