Laravel"无法添加外键约束" - 迁移

时间:2016-11-14 09:59:16

标签: laravel command foreign-keys migrate seed

我的外键有问题。 我有一个性别的用户:

我的迁移用户:

public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('nom');
            $table->string('prenom');
            $table->string('adresse');
            $table->integer('cp');
            $table->string('ville');
            $table->string('email')->unique();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
            $table->tinyInteger('admin')->nullable();
        });

    Schema::table('users', function ($table) {
        $table->integer('sexe_id')->unsigned();
        $table->foreign('sexe_id')->references('id')->on('sexes');
    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::drop('users');
}`

我的性别迁移:

/**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('sexes', function (Blueprint $table) {
            $table->increments('id');
            $table->string('libelle');
            $table->timestamps();
        });    
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('sexes');
    }

我的性别播种者:

class SexesTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        DB::table('sexes')->insert([
            [
                'libelle' => 'Homme',
                'created_at' => date('Y-m-d H:i:s'),
                'updated_at' => date('Y-m-d H:i:s'),
            ],
            [
                'libelle' => 'Femme',
                'created_at' => date('Y-m-d H:i:s'),
                'updated_at' => date('Y-m-d H:i:s'),
            ]
        ]);    
    }
}

我的用户播种机:

class UsersTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        DB::table('users')->insert([
            [
                'name' => 'admin',
                'nom' => 'Virlois',
                'prenom' => 'Peter',
                'sexe_id' => 1,
                'email' => 'admin@admin.fr',
                'adresse' => '12 rue Jean Rostand',
                'cp' => 90000,
                'ville' => 'Belfort',
                'password' => bcrypt('admin123'),
                'created_at' => date('Y-m-d H:i:s'),
                'updated_at' => date('Y-m-d H:i:s'),
                'admin' => 1,
            ],
            [
                'name' => 'test',
                'nom' => 'Mennegain',
                'prenom' => 'Mathieu',
                'sexe_id' => 1,
                'email' => 'test@test.fr',
                'adresse' => '12 rue Jean Rostand',
                'cp' => 90000,
                'ville' => 'Belfort',
                'password' => bcrypt('test123'),
                'created_at' => date('Y-m-d H:i:s'),
                'updated_at' => date('Y-m-d H:i:s'),
                'admin' => 0,
            ]
        ]);
    }
}

我的数据库播种机:

class DatabaseSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $this->call(SexesTableSeeder::class);
        $this->call(UsersTableSeeder::class);
        $this->call(SaisonTableSeeder::class);
        $this->call(ProduitTypeSeeder::class);
        $this->call(SportsTableSeeder::class);
        $this->call(ProduitsTableSeeder::class);
    }
}

当我运行时:php artisan migrate:refresh -seed

我有这个错误:

 [Illuminate\Database\QueryException]                                         
  SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL  
  : alter table `users` add constraint `users_sexe_id_foreign` foreign key (`  
  sexe_id`) references `sexes` (`id`))                                         



  [PDOException]                                                          
  SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint 

2 个答案:

答案 0 :(得分:1)

迁移的顺序非常重要。根据您发布的内容,您首先运行用户迁移,然后在同一迁移中尝试创建用户表并更改表。另一个表sexes不存在

($table->foreign('sexe_id')->references('id')->on('sexes');)

这就是你收到错误的原因。

我建议将迁​​移分开以便运行用户,性别,改变用户或性别,用户并改变相同的迁移,但这不是一种好办法,我的意思是混合迁移(创建和更改)

答案 1 :(得分:0)

id表中的sexes 1和sexe_id =>中可能没有任何内容1产生此错误。在UsersTableSeeder而非硬编码'sexe_id' => 1查询您的sexes表格中的任何行,并使用该动态id