【问题标题】:Laravel migration ("SQLSTATE[HY000]: error: 1005 Can't create table `laravel`.`orders` 150 "Foreign key constraint is incorrectly formed" please [duplicate]Laravel迁移(“SQLSTATE [HY000]:错误:1005无法创建表`laravel`.`orders`150“外键约束形成错误”请[重复]
【发布时间】:2022-01-25 21:22:47
【问题描述】:
 public function up()  {          
    Schema::create('orders', function (Blueprint $table) {          
         $table->increments('id');   
         $table->unsignedBigInteger('seller_id');   
         $table->foreign('seller_id')->references('id')->on('sellers')->onDelete('cascade');   
         $table->decimal('total',8,2);  
         $table->decimal('discount',8,2)->default(0);    
         $table->decimal('tax',8,2);   
         $table->boolean('paid')->default(0);          
         $table->timestamps();  
    }); 
}

卖家表

public function up() {  

    Schema::create('sellers', function (Blueprint $table) {
        
        $table->increments('id'); 
        $table->string('CompanyName');  
        $table->string('SellerName');  
        $table->string('email')->unique();  
        $table->string('password');
        $table->bigInteger('contact');
        $table->mediumText('officeAddress');
        $table->string('city');
        $table->string('state');
        $table->rememberToken();
        $table->timestamps();

    });

}

我尝试了所有可能的方法来定义外键但无法成功请帮助我 enter image description here

【问题讨论】:

    标签: laravel laravel-migrations


    【解决方案1】:

    increments()

    increments 方法创建一个自动递增的 UNSIGNED INTEGER 等效列作为主键:

    $table->increments('id');
    

    sellers 表上的id 列是使用$table->increments('id'); 定义的

    orders 表中的引用列seller_id必须具有匹配的数据类型。

    解决方案

    订单迁移。

    
    
    
     public function up()  {          
        Schema::create('orders', function (Blueprint $table) {          
             // ...  
             $table->unsignedInteger('seller_id');   
             // ...
              
        }); 
    }
    

    【讨论】:

    • 谢谢你的帮助
    猜你喜欢
    • 2020-12-19
    • 2020-12-02
    • 1970-01-01
    • 2021-08-06
    • 2021-07-27
    • 2021-12-24
    • 2019-07-27
    • 2020-11-27
    • 2020-08-07
    相关资源
    最近更新 更多