Skip to content

Instantly share code, notes, and snippets.

@Asuforce
Last active November 24, 2015 15:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Asuforce/853cdfc151381a74bb30 to your computer and use it in GitHub Desktop.
Save Asuforce/853cdfc151381a74bb30 to your computer and use it in GitHub Desktop.
Laravelで外部キー制約のあるテーブルを操作する。 ref: http://qiita.com/Asuforce/items/2c7cf0c95225083a2af5
$ php artisan db:seed
SQLSTATE[42000]: Syntax error or access violation: 1701 Cannot truncate a table referenced in a foreign key constraint
public function run()
{
Model::unguard();
DB::table('parents')->truncate();
$this->call('ParentsSeeder');
Model::reguard();
}
public function run()
{
Model::unguard();
DB::statement('SET FOREIGN_KEY_CHECKS=0;');
DB::table('parents')->truncate();
$this->call('ParentsSeeder');
DB::statement('SET FOREIGN_KEY_CHECKS=1;');
Model::reguard();
}
function run()
{
Schema::create('parents', function (Blueprint $table) {
$table->increments('id');
$table->string('email', 60)->unique();
$table->string('name', 30);
$table->timestamps();
$table->softDeletes();
});
Schema::create('children', function (Blueprint $table) {
$table->increments('id');
$table->string('email', 60);//FK
$table->foreign('email')->references('email')->on('parents')->onDelete('cascade');
$table->string('name', 30);
$table->timestamps();
$table->softDeletes();
});
{
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment