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

时间:2020-01-19 10:47:09

标签: php mysql laravel

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

代码

divs = soup.find_all('div', {'id': 'facts'})
for div in divs:
    fact_li = div.find_all('li')
    for fact in fact_li:
        spans = fact.find_all('span', {'class': 'description'})
        for span in spans:
            a = fact.find_all('a')
            result_list.append(a)

错误

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');
        });
    }

谢谢。

1 个答案:

答案 0 :(得分:3)

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