【问题标题】:SQLSTATE[HY000] Foreign key constraint is incorrectly formed error in Laravel 8SQLSTATE [HY000] Laravel 8 中的外键约束格式错误
【发布时间】:2021-08-18 04:17:47
【问题描述】:

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', 'detail', 'image','color','logo','user_id'
];

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

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

【问题讨论】:

  • 更改用户表 $table->id();到 $table->bigIncrements('id');
  • C:\xampp\htdocs\ContentBaseApp\vendor\laravel\framework\src\Illuminate\Database\Connection.php:471 PDOException::("SQLSTATE[HY000]: 一般错误:1005 Can' t 创建表laravel.projects (errno: 150 "外键约束格式不正确")")
  • 更改后显示此错误
  • PDOException::("SQLSTATE[HY000]: 一般错误: 1005 无法创建表laravel.projects (errno: 150 "外键约束格式不正确")") 之后更改显示此错误
  • 我已经在我的系统中验证了。如果你改变 $table->id();到 $table->bigIncrements('id');在用户迁移中它工作正常。我从您的问题中复制了相同的迁移

标签: laravel eloquent laravel-8 laravel-migrations


【解决方案1】:

您的问题很简单,您需要阅读并了解更多关于关系查询构建和表旋转多对多的信息

示例项目和用户

Schema::create('projects_user', function (Blueprint $table) {
    $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();
});

 php artisan make:migration create_projects_user_table --create=projects_user

很导入搜索create tables corrument用

检查是西班牙语请您搜索英语替代 youtube relation many to many

【讨论】:

    【解决方案2】:

    问题是在 Laravel 8+ 中创建的,通常所有的答案都与一些旧版本有关,我搜索了很多但没有得到 8.4x 版本的解决问题,最后我尝试了不同的方法解决这个问题,现在在尝试了多种方法后,我找到了解决方案。 尝试更改这些文件 Product.php 和 Project.php 文件。 100% 对你有用。

    产品表(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')->nullable()->index();//Change this line of Code
        $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')->nullable()->index(); //Change this line of Code
       $table->unsignedBigInteger('product_id')->nullable()->index();//Change this line of Code
       $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();
    });
    

    }

    【讨论】:

      【解决方案3】:

      对于项目表,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();
      });
      

      【讨论】:

      • C:\xampp\htdocs\ContentBaseApp\vendor\laravel\framework\src\Illuminate\Database\Connection.php:471 PDOException::("SQLSTATE[HY000]: 一般错误:1005 Can' t create table laravel.projects (errno: 150 "Foreign key constraint is wrongly forms")") 这是现在的错误
      • 为什么在您的模型中使用 getUser() ,我的意思是使用 get 使其成为访问器,这是另一回事
      • 你可以试试 user() 和 products() 还有 projects() ,你需要一个 s ,这样 laravel 可以理解关系和猜表
      猜你喜欢
      • 2021-08-26
      • 1970-01-01
      • 2020-01-06
      • 2018-07-29
      • 1970-01-01
      • 2019-08-02
      • 2021-07-03
      • 2020-01-14
      • 2022-11-18
      相关资源
      最近更新 更多