0
我想我的種子數據庫Laravel 5.遷移成功,但是試圖種子時,我得到一個錯誤:完整性約束違規在Laravel播種數據庫時5
[PDOException]
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`ict302`.`psas`, CONSTRAINT `psas_psa_user_
id_foreign` FOREIGN KEY (`psa_user_id`) REFERENCES `users` (`id`))
下面是我的遷移:
用戶表:
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password')->nullable();
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
DB::statement('SET FOREIGN_KEY_CHECKS=0;');
Schema::dropIfExists('users');
DB::statement('SET FOREIGN_KEY_CHECKS=1;');
}
PSA表:
public function up()
{
Schema::create('psas', function (Blueprint $table) {
$table->increments('psa_id');
$table->timestamps();
$table->string('psa_email',50);
$table->unique('psa_email');
$table->string('psa_name', 30);
$table->integer('psa_user_id')->unsigned()->onDelete('cascade');
});
Schema::table('psas', function (Blueprint $table)
{
$table->foreign('psa_user_id')->references('id')->on('users');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
DB::statement('SET FOREIGN_KEY_CHECKS=0;');
Schema::dropIfExists('psas');
DB::statement('SET FOREIGN_KEY_CHECKS=1;');
}
數據庫播種機類:
public function run()
{
//Model::unguard();
DB::statement('SET FOREIGN_KEY_CHECKS=0;');
$this->call(AccessTableSeeder::class);
$this->call(HistoryTypeTableSeeder::class);
$this->call(UserTableSeeder::class);
$this->call(PsaTableSeeder::class);
DB::statement('SET FOREIGN_KEY_CHECKS=1;');
//Model::reguard();
}
什麼我做錯了,我該如何解決?
什麼是您的數據庫模式? – GordonM