Laravel 迁移不兼容的外键

Y.F*_*kov 5 mysql laravel eloquent

我被告知要删除我们其中一个表的自动递增功能,但这开始导致问题:

PDOException::(“SQLSTATE[HY000]:一般错误:3780 引用列“limitCardId”和外键约束“rolling_control_cards_limitcardid_foreign”中引用的列“id”不兼容。”)

以下是有问题的键:

PK$table->integer('id');

氟肯$table->integer('limitCardId')->unsigned()->index();

关系$table->foreign('limitCardId')->references('id')->on('limit_cards');

我读过其他几篇文章,提出了几种解决方案,但是没有一个解决我的问题,这里列出了我已经尝试过的内容:

在我的基表模型中定义它:

    protected $primaryKey = 'id';
    public $incrementing = false;
Run Code Online (Sandbox Code Playgroud)

Schema::disableForeignKeyConstraints();在方法的开头添加 up(), Schema::enableForeignKeyConstraints();最后,我运行了composer dump-autoload, php artisan config:clear, php artisan cache:clear。

正如建议的,这是两个表的精简版本,基表:

 public function up()
    {
        Schema::create('limit_cards', function (Blueprint $table) {
            $table->integer('id');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('limit_cards');
    }
Run Code Online (Sandbox Code Playgroud)

相关表:

    public function up()
    {
        Schema::create('rolling_control_cards', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('limitCardId')->unsigned()->index();
            $table->unique(['controlCardNumber', 'limitCardId']);
            $table->timestamps();

            $table->foreign('limitCardId')->references('id')->on('limit_cards');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('rolling_control_cards');
    }
Run Code Online (Sandbox Code Playgroud)

dan*_*oon 9

为了使外键引用兼容,它们必须具有完全相同的类型,包括它们的符号。

$table->integer('id');默认情况下创建有符号整数列。要么让它不签名以匹配limitCardId,要么让它们都签名。