Last active
November 24, 2015 15:59
-
-
Save Asuforce/853cdfc151381a74bb30 to your computer and use it in GitHub Desktop.
Laravelで外部キー制約のあるテーブルを操作する。 ref: http://qiita.com/Asuforce/items/2c7cf0c95225083a2af5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ php artisan db:seed |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SQLSTATE[42000]: Syntax error or access violation: 1701 Cannot truncate a table referenced in a foreign key constraint |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public function run() | |
{ | |
Model::unguard(); | |
DB::table('parents')->truncate(); | |
$this->call('ParentsSeeder'); | |
Model::reguard(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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