<i id='nMTiD'><tr id='nMTiD'><dt id='nMTiD'><q id='nMTiD'><span id='nMTiD'><b id='nMTiD'><form id='nMTiD'><ins id='nMTiD'></ins><ul id='nMTiD'></ul><sub id='nMTiD'></sub></form><legend id='nMTiD'></legend><bdo id='nMTiD'><pre id='nMTiD'><center id='nMTiD'></center></pre></bdo></b><th id='nMTiD'></th></span></q></dt></tr></i><div id='nMTiD'><tfoot id='nMTiD'></tfoot><dl id='nMTiD'><fieldset id='nMTiD'></fieldset></dl></div>

    <bdo id='nMTiD'></bdo><ul id='nMTiD'></ul>

      <small id='nMTiD'></small><noframes id='nMTiD'>

    1. <legend id='nMTiD'><style id='nMTiD'><dir id='nMTiD'><q id='nMTiD'></q></dir></style></legend><tfoot id='nMTiD'></tfoot>

        Laravel 迁移(errno: 150 “外键约束格式错误")

        时间:2023-10-09

        <i id='gRLZv'><tr id='gRLZv'><dt id='gRLZv'><q id='gRLZv'><span id='gRLZv'><b id='gRLZv'><form id='gRLZv'><ins id='gRLZv'></ins><ul id='gRLZv'></ul><sub id='gRLZv'></sub></form><legend id='gRLZv'></legend><bdo id='gRLZv'><pre id='gRLZv'><center id='gRLZv'></center></pre></bdo></b><th id='gRLZv'></th></span></q></dt></tr></i><div id='gRLZv'><tfoot id='gRLZv'></tfoot><dl id='gRLZv'><fieldset id='gRLZv'></fieldset></dl></div>
          <tbody id='gRLZv'></tbody>
      1. <small id='gRLZv'></small><noframes id='gRLZv'>

          <bdo id='gRLZv'></bdo><ul id='gRLZv'></ul>

              <legend id='gRLZv'><style id='gRLZv'><dir id='gRLZv'><q id='gRLZv'></q></dir></style></legend>
              <tfoot id='gRLZv'></tfoot>

                • 本文介绍了Laravel 迁移(errno: 150 “外键约束格式错误")的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个订单表和一个 sell_shipping_labels ,它引用了 orders.id 作为外部对象.但是,当我运行 Laravel 迁移时,我得到了可怕的错误代码:

                  I have an orders table and a have a sell_shipping_labels which references orders.id as a foreign. However when I run the Laravel migration I get the dreaded error code:

                  [IlluminateDatabaseQueryException]
                  SQLSTATE[HY000]: General error: 1005 Can't create table cheapbooks_test.#sql-b5b_b2a (errno: 150 "外键约束的格式不正确") (SQL:更改表 sell_shipping_labels 添加约束 sell_shipping_labels_order_id_foreign 外键 (order_id) 引用 orders (id))

                  [IlluminateDatabaseQueryException]
                  SQLSTATE[HY000]: General error: 1005 Can't create table cheapbooks_test.#sql-b5b_b2a (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table sell_shipping_labels add constraint sell_shipping_labels_order_id_foreign foreign key (order_id) references orders (id))

                  [DoctrineDBALDriverPDOException]
                  SQLSTATE[HY000]: 一般错误: 1005 无法创建表 cheapbooks_test.#sql-b5b_b2a (errno: 150 "外键约束形成不正确")

                  [DoctrineDBALDriverPDOException]
                  SQLSTATE[HY000]: General error: 1005 Can't create table cheapbooks_test.#sql-b5b_b2a (errno: 150 "Foreign key constraint is incorrectly formed")

                  这是我的orders表架构:

                     Schema::create('orders', function (Blueprint $table) {
                          $table->increments('id');
                          $table->integer('user_id');
                          $table->integer('book_id');
                          $table->integer('status_id');
                          $table->double('payment_amount')->nullable();
                          $table->timestamp('received_at')->nullable();
                          $table->timestamp('paid_at')->nullable();
                          $table->timestamps();
                          $table->softDeletes();
                      });
                  

                  这是我的sell_shipping_labels架构:

                  Schema::create('sell_shipping_labels', function (Blueprint $table) {
                          $table->increments('id');
                          $table->unsignedInteger('order_id');
                          $table->string('shippo_object_id');
                          $table->string('label_url');
                          $table->string('tracking_url');
                          $table->string('tracking_number');
                          $table->timestamp('arrived_at');
                          $table->timestamps();
                          $table->softDeletes();
                  
                          $table->foreign('order_id')->references('id')->on('orders');
                      });
                  }
                  

                  现在我颠倒了互联网,试图找出问题所在.所有关于这个问题的帖子都提到了一个事实,即必须在具有外键的表之前创建订单表,但这对我来说不是问题,因为我的文件在正确的顺序.

                  Now I've flipped the internet upside down trying to figure out the problem. All of the post about this problem all refer to the fact that the orders table must be created BEFORE the table that has the foreign key on it but this isn't a problem for me because my files are in the correct order.

                  推荐答案

                  由于 increments() 创建了一个无符号整数列,因此您需要将外键列也定义为无符号整数.

                  Since increments() creates an unsigned integer column, you need to define the foreign key column as unsigned integer too.

                  Laravel 6+ 中的默认迁移使用 bigIncrements(),所以你需要使用 unsignedBigInteger() 方法:

                  Default migrations in Laravel 6+ use bigIncrements(), so you need to use unsignedBigInteger() method:

                  $table->unsignedBigInteger('order_id');
                  

                  https://laravel.com/docs/6.x/迁移#foreign-key-constraints

                  对于旧版本 Laravel 中的默认迁移,使用 unsignedInteger() 方法:

                  For default migrations in older versions of Laravel use unsignedInteger() method:

                  $table->unsignedInteger('order_id');
                  

                  或者:

                  $table->integer('order_id')->unsigned();
                  

                  https://laravel.com/docs/5.5/migrations#foreign-关键约束

                  这篇关于Laravel 迁移(errno: 150 “外键约束格式错误")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何在 Laravel 5 中使用 Eloquent 更新数据透视表 下一篇:使用 Laravel 队列时如何避免作业数据库表锁定问题?

                  相关文章

                  <tfoot id='uJeQI'></tfoot>

                  • <bdo id='uJeQI'></bdo><ul id='uJeQI'></ul>

                    <small id='uJeQI'></small><noframes id='uJeQI'>

                    <legend id='uJeQI'><style id='uJeQI'><dir id='uJeQI'><q id='uJeQI'></q></dir></style></legend>
                    1. <i id='uJeQI'><tr id='uJeQI'><dt id='uJeQI'><q id='uJeQI'><span id='uJeQI'><b id='uJeQI'><form id='uJeQI'><ins id='uJeQI'></ins><ul id='uJeQI'></ul><sub id='uJeQI'></sub></form><legend id='uJeQI'></legend><bdo id='uJeQI'><pre id='uJeQI'><center id='uJeQI'></center></pre></bdo></b><th id='uJeQI'></th></span></q></dt></tr></i><div id='uJeQI'><tfoot id='uJeQI'></tfoot><dl id='uJeQI'><fieldset id='uJeQI'></fieldset></dl></div>