繁体   English   中英

Laravel 一般错误:1005 无法创建表`categories_products`(错误号:150“外键约束的格式不正确”)

[英]Laravel General error: 1005 Can't create table `categories_products` (errno: 150 "Foreign key constraint is incorrectly formed")

尝试在 Laravel 中迁移并弹出此错误。 我认为错误可能是 bigInteger 类型,因为我在另一个相同的问题答案中发现。 请参阅将 bigInteger 更改为 bigIncrements,但我没有遇到有关类型约束的另一个错误。

代码

public function up()
    {
        Schema::create('categories_products', function (Blueprint $table) {
            $table->bigInteger('category_id')->unsigned()->index();
            $table->bigInteger('product_id')->unsigned()->index();

            $table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
            $table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
        });
    }

错误

[Illuminate\Database\QueryException]
  SQLSTATE[HY000]: General error: 1005 Can't create table `categories_products` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table `categori
  es_products` add constraint `categories_products_category_id_foreign` foreign key (`category_id`) references `categories` (`id`) on delete cascade)



  [Doctrine\DBAL\Driver\PDOException]
  SQLSTATE[HY000]: General error: 1005 Can't create table `categories_products` (errno: 150 "Foreign key constraint is incorrectly formed")



  [PDOException]
  SQLSTATE[HY000]: General error: 1005 Can't create table `categories_products` (errno: 150 "Foreign key constraint is incorrectly formed")

谢谢。

如果没有看到关系的目标表上的列定义,很难给出明确的答案。 类型需要匹配。 因此,如果您在 id 字段中使用 bigIncrements(),那么unsignedBigInteger()bigInteger()->unsigned()都应该用作外键的类型,但是如果您使用increments作为 id 则外键需要是unsignedInteger()integer()->unsigned()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM