duangu3620 2018-03-19 20:12
浏览 53
已采纳

MySQL / Laravel外键约束形成错误

I am trying to run php artisan migrate to create my mysql tables usin laravel.

I got this error: Foreign key constraint is incorrectly formed

Users Table:

Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('street');
            $table->string('city');
            $table->string('phone');
            $table->string('email')->unique();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });

password_resets table:

Schema::create('password_resets', function (Blueprint $table) {
            $table->string('email')->index();
            $table->string('token');
            $table->timestamp('created_at')->nullable();
        });

products table:

 Schema::create('products', function (Blueprint $table) {
            $table->increments('id');
            $table->string('product_type');
            $table->integer('quantity');
            $table->timestamps();
        });

Shipments table:

  Schema::create('shipments', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('order_number')->unsigned();
        $table->integer('product_id')->unsigned();
        $table->foreign('order_number')->references('id')->on('orders');
        $table->foreign('product_id')->references('id')->on('products');
        $table->dateTime('chargecardtime');
        $table->dateTime('packingtime');
        $table->date('shiporderdate');
        $table->timestamps();
    });

Orders table:

    Schema::create('orders', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('customer_id')->unsigned();
        $table->integer('product_id')->unsigned();
        $table->foreign('customer_id')->references('id')->on('users');
        $table->foreign('product_id')->references('id')->on('products');
        $table->string('name');
        $table->string('to_street');
        $table->string('to_city');
        $table->date('ship_date');  
        $table->string('phone');
        $table->timestamps();
    });

Exception trace:

1 PDOException::("SQLSTATE[HY000]: General error: 1005 Can't create table ec.#sql-3664_86 (errno: 150 "Foreign key constraint is incorrectly formed")")

i guess there is a problem with orders table since after error i cant see that table in database.others are created.

  • 写回答

2条回答 默认 最新

  • doushenyu2537 2018-03-19 20:15
    关注

    Since you're referencing an id you need to make the foreign key column unsigned. As ids are by default unsigned (non-negative).

    So do this for all your foreign keys:

    $table->integer('product_id')->unsigned();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作