无法添加外键

时间:2019-04-01 18:32:25

标签: php mysql laravel

我写了一些迁移文件,但是不会添加外键,这给我带来了很小的错误。 我看不到一切可疑。

一般错误:1251无法添加外键约束(SQL:alter table'advertising'添加约束'advertising_category_id_foreign'外键('category_id')在更新级联上的删除限制中引用了'category'('id'))

我在相关迁移中更改了id类型,但是没有用。

我将相关迁移的ID更改为index(),但是agane无效。

public function up()
    {
        Schema::create('advertising', function (Blueprint $table){
            $table->increments('id')->index();
            $table->integer('user_id')->unsigned()->index();
            $table->string('ranking')->nullable();
            $table->string('status')->nullable();
            $table->string('name')->nullable();
            $table->string('family')->nullable();
            $table->string('advertising_name')->nullable();
            $table->string('phone')->nullable();
            $table->string('email')->nullable();
            $table->string('address')->nullable();
            $table->string('country-city')->nullable();
            $table->integer('category_id')->unsigned()->nullable();
            $table->string('week_days')->nullable();
            $table->string('time_open')->nullable();
            $table->string('time_close')->nullable();
            $table->string('body')->nullable();
            $table->string('facebook')->nullable();
            $table->string('twitter')->nullable();
            $table->string('instagram')->nullable();
            $table->integer('service_garanti_id')->unsigned()->nullable();
            $table->integer('professional_id')->unsigned()->nullable();
            $table->integer('insurance_Limit_id')->unsigned()->nullable();
            $table->timestamps();


            $table->foreign('user_id')->references('id')->on('users')
            ->onDelete('cascade')->onUpdate('cascade');
            $table->foreign('category_id')->references('id')->on('category')
            ->onDelete('restrict')->onUpdate('cascade');
            $table->foreign('service_garanti_id')->references('id')
            ->on('service_garanti')
            ->onDelete('restrict')->onUpdate('cascade');
            $table->foreign('professional_id')->references('id')
            ->on('professional')->onDelete('restrict')->onUpdate('cascade');
            $table->foreign('insurance_Limit_id')->references('id')
            ->on('insurance_limit')
            ->onDelete('restrict')->onUpdate('cascade');
        });
相关迁移:
public function up()
    {
        Schema::create('category', function (Blueprint $table){
             $table->increments('id');
             $table->string('body')->nullable();
             $table->timestamps();

        });
public function up()
    {
        Schema::create('service_garanti', function (Blueprint $table){
            $table->increments('id')->index();
            $table->string('body')->nullable();
            $table->timestamps();
        });
public function up()
    {
        Schema::create('Professional', function (Blueprint $table){
            $table->increments('id')->index;
            $table->string('body')->nullable();
            $table->timestamps();
        });
    }
public function up()
    {
        Schema::create('Insurance_Limit', function (Blueprint $table){
            $table->increments('id')->index();
            $table->string('body')->nullable();
            $table->timestamps();
        });
    }

1 个答案:

答案 0 :(得分:0)

尝试更改迁移顺序,以便首先创建包含外来值的表(类别,service_garanti等),然后创建广告表并进行关键连接。为了使迁移成功,表和列必须已经存在。

如果您知道密钥正确并且将被创建,则另一个选择是通过将其添加到迁移的顶部来仅在运行时禁用密钥检查。

public function up(){
   Schema::disableForeignKeyConstraints();

   Schema::create('advertising', function (Blueprint $table){
      ...
   }
}