【问题标题】:Laravel 9 migration fails after switching to mysql切换到mysql后Laravel 9迁移失败
【发布时间】:2022-11-01 13:22:09
【问题描述】:

我有一个本地开发的带有 sqlite 的 laravel 项目。出于部署原因,我想切换到 mysql。不幸的是,我的关系迁移不再起作用并产生以下错误(我已确保它们运行的​​顺序正确,所有其他必需的表首先生成并且看起来正确)

Can't create table `laraveltest`.`test1s_test2s` (errno: 150 "Foreign key constraint is incorrectly formed") 
(SQL: alter table `test1s_test2s` add constraint `test1s_test2s_test1_id_foreign` foreign key (`suacap_id`) 
references `test1s` (`id`) on delete cascade)

迁移如下所示:

测试1

public function up()
{
    Schema::create('test1s', function (Blueprint $table) {
        $table->id();
...

测试2

public function up()
{
    Schema::create('test2s', function (Blueprint $table) {
        $table->id();
...

关系表 test1s_test2s

public function up()
{
    Schema::create('test1s_test2s', function (Blueprint $table) {
        $table->primary(['test1_id', 'test2_id']);
        $table->string('test1_id');
        $table->foreign('test1_id')
            ->references('id')
            ->on('test1s')->onDelete('cascade');
        $table->string('test2_id');
        $table->foreign('test2_id')
            ->references('id')
            ->on('test2s')->onDelete('cascade');
    });
}

我猜这与未签名的主键有关,而其他表的 bigInt id 是?我尝试修改

$table->primary(['test1_id', 'test2_id'])->unsigned();

但这不起作用。

有人可以指出我正确的方向吗?谢谢

【问题讨论】:

    标签: mysql laravel laravel-9


    【解决方案1】:

    想想每当你做一些外键时,它们应该是 UNSIGNED BIGINT 而不是字符串,在 Laravel 9 - $table->foreignId('user_id'); 另请阅读官方文档Laravel Official Doc

    【讨论】:

      猜你喜欢
      • 2014-10-26
      • 1970-01-01
      • 2016-02-16
      • 2017-06-23
      • 2016-06-28
      • 1970-01-01
      • 2020-04-09
      • 1970-01-01
      相关资源
      最近更新 更多