Laravel - 错误:150"外键约束形成错误")

时间:2018-02-25 12:59:31

标签: php laravel

我读过一些关于这个错误的话题,我添加了 - > unsigned()但是当我运行" php artisan migrate:fresh"它有同样的错误。请帮帮我

Schema::create('unlock_video', function (Blueprint $table) {
            $table->increments('unlock_video_id')->unsigned();
            $table->integer('unlock_video_category_id')->unsigned();
            $table->string('unlock_video_name');
            $table->string('unlock_video_alias')->unique();
            $table->string('unlock_video_code');
            $table->integer('unlock_video_point')->default(0)->unsigned();
            $table->string('unlock_video_mini_poster')->nullable();
            $table->string('unlock_video_poster')->nullable();
            $table->text('unlock_video_content')->nullable();
            $table->bigInteger('unlock_video_view')->default(0);
            $table->smallInteger('unlock_video_wait_confirm')->default(0);
            $table->smallInteger('unlock_video_status')->default(0);
            $table->string('unlock_video_download_id')->nullable();
            $table->smallInteger('unlock_video_accept_bpoint')->default(0);
            $table->integer('unlock_video_real_folder_id')->unsigned();
            $table->text('unlock_video_log')->nullable();
            $table->string('unlock_video_head_title')->nullable();
            $table->string('unlock_video_meta_keyword')->nullable();
            $table->string('unlock_video_meta_description')->nullable();
            $table->integer('unlock_video_unlock_account')->unsigned();
            $table->integer('unlock_video_account_id')->unsigned();
            $table->timestamps();

            $table->foreign('unlock_video_unlock_account')->references('unlock_account_id')->on('unlock_account');
            $table->foreign('unlock_video_account_id')->references('id')->on('users');
        });

Schema::create('unlock_account', function (Blueprint $table) {
            $table->increments('unlock_account_id')->unsigned();
            $table->string('unlock_account_name');
            $table->string('unlock_account_password');
            $table->timestamps();
        });

错误:

Illuminate\Database\QueryException  : SQLSTATE[HY000]: General error: 1005 Can't create table `test`.`#sql-1880_26` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table `unlock_video` add constraint `unlock_video_unlock_account_foreign` foreign key (`unlock_video_unlock_account`) references `unlock_account` (`unlock_account_id`))

1 个答案:

答案 0 :(得分:1)

更改表创建顺序。

首先创建unlock_account表,然后使用外键创建unlock_video表...