【问题标题】:Laravel migration incompatible error happen. but both types seem to be compatible发生 Laravel 迁移不兼容错误。但两种类型似乎都兼容
【发布时间】:2021-06-09 02:59:48
【问题描述】:

这是我的数据库和migration.php

我想将category_id 设置为foreign key 但发生不兼容错误。

我不知道为什么它不兼容。我该如何解决?

mysql> desc category;
+--------------+--------------+------+-----+---------+----------------+
| Field        | Type         | Null | Key | Default | Extra          |
+--------------+--------------+------+-----+---------+----------------+
| id           | int unsigned | NO   | PRI | NULL    | auto_increment |
| name         | varchar(256) | YES  |     | NULL    |                |
| type         | char(1)      | YES  |     | NULL    |                |
| order        | int          | YES  |     | NULL    |                |
public function up()
{
    Schema::create('peaces', function (Blueprint $table) {
        $table->bigIncrements('id')->unique();
        $table->bigInteger('category_id')->unsigned();
        $table->Integer('price');
        $table->timestamps();

        $table->foreign('category_id')
            ->references('id')
            ->on('category');
    });
}

异常跟踪:

PDOException::("SQLSTATE[HY000]: 一般错误:3780 在外键约束 'peaces_category_id_foreign' 中引用列 'category_id' 和引用列 'id' 不兼容。")

【问题讨论】:

标签: laravel


【解决方案1】:

我认为在category 表中category idinteger,但在peaces 架构中,您已将category_id 定义为big integer,所以它的抛出错误。

所以最好将category table id 更新为big integer 以避免错误或反之亦然

   Schema::create('category', function (Blueprint $table) {
        $table->bigIncrements('id');

【讨论】:

    猜你喜欢
    • 2013-09-23
    • 2011-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多