微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

SQLSTATE[HY000] Laravel 8 中的外键约束格式错误

如何解决SQLSTATE[HY000] Laravel 8 中的外键约束格式错误

sqlSTATE[HY000]:一般错误:1005 无法创建表 laravel.projects (errno: 150 "外键约束是 格式不正确")

当我迁移我的项目表并尝试连接三个表时出现上述错误

  1. 一个用户有很多产品,产品有自己的 id。
  2. 一个产品有很多项目,项目有自己的id。

用户表 (user.PHP)

public function up()
{
    Schema::create('users',function (Blueprint $table) {
        $table->id();
        $table->string('name');
        $table->string('email')->unique();
        $table->timestamp('email_verified_at')->nullable();
        $table->string('password');
        $table->rememberToken();
        $table->foreignId('current_team_id')->nullable();
        $table->text('profile_photo_path')->nullable();
        $table->timestamps();
    });
}

产品表 (product.PHP)

public function up()
{
    Schema::create('products',function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('name');
        $table->text('detail');
        $table->string('color');
        $table->string('image');
        $table->string('logo');
        $table->unsignedBigInteger('user_id');
        $table->timestamps();
        $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
    });
}

项目表 (project.PHP)

Schema::create('projects',function (Blueprint $table) {
    // $table->('id');
    $table->bigIncrements('id');
    $table->string('name',255)->nullable();
    $table->string('detail',500)->nullable();
    $table->string('color',255)->nullable();
    $table->string('image',22)->nullable();
    $table->string('logo',22)->nullable();
    $table->unsignedBigInteger('user_id');
    $table->unsignedBigInteger('product_id');
    $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
    $table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
    $table->timestamp('created_at')->useCurrent();
    $table->timestamp('updated_at')->nullable();
});

用户模型

public function getProducts()
{
    return $this->hasMany('App\Models\Product');
}

public function getProject()
{
    return $this->hasMany('App\Models\Project');
}

产品型号

use HasFactory;

protected $fillable = [
    'name','detail','image','color','logo','user_id'
];

public function getUser()
{
    return $this->belongsTo(User::class);
}

项目模型

use HasFactory;

protected $fillable = [
    'name','user_id'
];

public function getUser(){
    return $this->belongsTo(User::class);
}

我还需要帮助才能使我的模型正常工作。

解决方法

对于项目表,Laravel 会自己解决。

Schema::create('projects',function (Blueprint $table) {
    // $table->('id');
    $table->bigIncrements('id');
    $table->string('name',255)->nullable();
    $table->string('detail',500)->nullable();
    $table->string('color',255)->nullable();
    $table->string('image',22)->nullable();
    $table->string('logo',22)->nullable();
    $table->foreignId('user_id')
        ->constrained()
        ->onDelete('cascade');
    
    $table->foreignId('product_id')
        ->constrained()
        ->onDelete('cascade');

    $table->timestamp('created_at')->useCurrent();
    $table->timestamp('updated_at')->nullable();
});

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。