Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
laravel 8外键_Laravel_Migration_Laravel 8 - Fatal编程技术网

laravel 8外键

laravel 8外键,laravel,migration,laravel-8,Laravel,Migration,Laravel 8,我尝试迁移具有外键的表。每次迁移表时,都会产生一个错误,即: 常规错误:1215无法添加外键约束 以下是我的表迁移: Schema::create('profile_pictures', function (Blueprint $table) { $table->bigIncrements('id'); $table->bigInteger('user_id')->nullable(); $table->binary('image')->nu

我尝试迁移具有外键的表。每次迁移表时,都会产生一个错误,即:

常规错误:1215无法添加外键约束

以下是我的表迁移:

Schema::create('profile_pictures', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->bigInteger('user_id')->nullable();
    $table->binary('image')->nullable();
    $table->timestamps();

    $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
Schema::create('users', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->string('username');
    $table->string('first_name')->nullable();
    $table->string('last_name')->nullable();
    $table->string('email')->unique();
    $table->string('phone')->nullable();
    $table->timestamp('email_verified_at')->nullable();
    $table->string('password');
    $table->rememberToken();
    $table->timestamps();
});
这是我的模型:

class ProfilePicture extends Model
{
    protected $fillable = [
        'user_id',
        'image'
    ];

    public function user()
    {
        $this->belongsTo(User::class, 'user_id', 'id');
    }
}
以下是我的用户表迁移:

Schema::create('profile_pictures', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->bigInteger('user_id')->nullable();
    $table->binary('image')->nullable();
    $table->timestamps();

    $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
Schema::create('users', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->string('username');
    $table->string('first_name')->nullable();
    $table->string('last_name')->nullable();
    $table->string('email')->unique();
    $table->string('phone')->nullable();
    $table->timestamp('email_verified_at')->nullable();
    $table->string('password');
    $table->rememberToken();
    $table->timestamps();
});
根据,外键列必须具有 相同的数据类型+相同的长度+相同的比例 作为相应的引用列

我认为你应该使用

$table->unsignedBigInteger('user_id')->nullable(); 
而不是

$table->bigInteger('user_id')->nullable();
根据,外键列必须具有 相同的数据类型+相同的长度+相同的比例 作为相应的引用列

我认为你应该使用

$table->unsignedBigInteger('user_id')->nullable(); 
而不是

$table->bigInteger('user_id')->nullable();

将用户id列从bigInteger更新为UnsignedBigInteger,因为PK和FK需要相同的数据类型和长度

Schema::create('profile_pictures', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->unsignedBigInteger('user_id');
    $table->binary('image')->nullable();
    $table->timestamps();

    $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
我建议遵循惯例,将foreignd()方法与constrated()一起使用

样本(来自文件):


您可以在此处获得更多详细信息:

将用户id列从bigInteger更新为UnsignedBigInteger,因为PK和FK需要相同的数据类型和长度

Schema::create('profile_pictures', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->unsignedBigInteger('user_id');
    $table->binary('image')->nullable();
    $table->timestamps();

    $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
我建议遵循惯例,将foreignd()方法与constrated()一起使用

样本(来自文件):


您可以在此处获得更多详细信息:

我们还需要查看
用户
表的迁移情况,以便检查“id”字段类型我已经将用户迁移表放入try
$table->unsignedbiginger('user_id')->nullable()错误已消失,但用户id为空您认为用户id为空是什么意思?迁移只是为了创建DB表,而不是插入数据。我们还需要查看
用户
表的迁移情况,只是为了检查“id”字段类型我已经将用户迁移表放入try
$table->unsignedbiginger('user_id')->nullable()错误已消失,但用户id为空您认为用户id为空是什么意思?迁移只是为了创建数据库表,而不是插入数据。