<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 “外键约束格式错误")

        Laravel migration (errno: 150 quot;Foreign key constraint is incorrectly formedquot;)(Laravel 迁移(errno: 150 “外键约束格式错误))

        <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 “外键约束格式错误")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How to update a record using sequelize for node?(如何使用节点的 sequelize 更新记录?)
                  How to provide a mysql database connection in single file in nodejs(如何在 nodejs 中的单个文件中提供 mysql 数据库连接)
                  Looping Over Result Sets in MySQL(在 MySQL 中循环结果集)
                  Writing an SQL query to SELECT item from the following table(编写 SQL 查询以从下表中选择项目)
                  Converting MySQL code to Access: GROUP_CONCAT and a triple JOIN(将 MySQL 代码转换为 Access:GROUP_CONCAT 和三重 JOIN)
                  How can I convert an MDB (Access) file to MySQL (or plain SQL file)?(如何将 MDB (Access) 文件转换为 MySQL(或普通 SQL 文件)?)

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

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

                          <tbody id='uJeQI'></tbody>

                        <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>