无法在Laravel中添加外键

无法在Laravel中添加外键,laravel,foreign-keys,migration,primary-key,Laravel,Foreign Keys,Migration,Primary Key,当我尝试移植时,我得到一个错误,外键约束的格式不正确,但我不明白为什么 第一桌 Schema::create('uciteles', function (Blueprint $table) { $table->bigIncrements('id'); $table->string('name'); $table->integer('admin'); $table->string('predmet');

当我尝试移植时,我得到一个错误,外键约束的格式不正确,但我不明白为什么

第一桌

    Schema::create('uciteles', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('name');
        $table->integer('admin');
        $table->string('predmet');
        $table->string('email')->unique();
        $table->timestamp('email_verified_at')->nullable();
        $table->rememberToken();
        $table->timestamps();

        $table->unsignedBigInteger('user_id');

        $table->foreign('user_id')->references('id')->on('users');
    });
第二张桌子

Schema::create('users', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('email')->unique();
        $table->timestamp('email_verified_at')->nullable();
        $table->rememberToken();
        $table->timestamps();

    });

列的类型必须与外键引用中的类型相同,并且您需要自己创建列


以下是对您的问题的解释:

我认为您首先创建uciteles表,然后创建users表。 所以在这里更改格式。首先创建用户表,然后创建uciteles表

在迁移过程中更改创建表的时间,如2019\u 09\u 27\u 000000\u create\u users\u table.php更改日期和月份,然后users table首先创建uciteles表,其次创建uciteles表,或者取消外键检查并启用它

DB::statement('SET FOREIGN_KEY_CHECKS=0;');
Schema::create('uciteles', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('name');
        $table->integer('admin');
        $table->string('predmet');
        $table->string('email')->unique();
        $table->timestamp('email_verified_at')->nullable();
        $table->rememberToken();
        $table->timestamps();

        $table->unsignedBigInteger('user_id');

        $table->foreign('user_id')->references('id')->on('users');
    });
DB::statement('SET FOREIGN_KEY_CHECKS=1;');


我尝试将'id'更改为increments,将'user\u id'更改为integer,但仍然得到相同的错误。谢谢,但现在我得到的错误是,字段'user\u id'没有我尝试执行的默认值->可为null,但不起作用。将其设置为null&php artistan migrate:refresh