【问题标题】:Ubuntu laravel production migration not workingUbuntu laravel 生产迁移不起作用
【发布时间】:2020-12-05 12:04:45
【问题描述】:

迁移在我的本地服务器上运行良好,但在我的生产服务器(即 Ubuntu 18.04)上显示此错误,很奇怪它在我的本地主机上运行良好但不是在这里,我已经四处寻找可能的解决方案但没有为我工作,我希望你们中的一个可以帮助我解决这个问题!

我会在迁移出错的地方,以及 MySQL 错误和我找到一个命令,您可以在其中检查最新的 MySQL 外键错误!

Brands migration:

public function up()
{
    Schema::create('brands', function (Blueprint $table) {
        $table->id();
        $table->string('brand_name')->unqiue;
        $table->integer('categories_id')->unsigned();
        $table->foreign('categories_id')
        ->references('id')->on('categories')
        ->onDelete('cascade');
        $table->boolean('brand_hidden');
        $table->string('brand_color');
        $table->timestamps();
    });
}
php artisan migrate:fresh:

Dropped all tables successfully.
Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table
Migrated:  2014_10_12_000000_create_users_table (0 seconds)
Migrating: 2014_10_12_100000_create_password_resets_table
Migrated:  2014_10_12_100000_create_password_resets_table (0 seconds)
Migrating: 2019_08_19_000000_create_failed_jobs_table
Migrated:  2019_08_19_000000_create_failed_jobs_table (0 seconds)
Migrating: 2020_07_08_080611_create_categories_table
Migrated:  2020_07_08_080611_create_categories_table (0 seconds)
Migrating: 2020_07_08_124956_create_brands_table

   Illuminate\Database\QueryException

  SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table `brands` add constraint `brands_categories_id_foreign` foreign key (`categories_id`) references `categories` (`id`) on delete cascade)


Latest forein key error:

2020-12-05 12:48:15 0x7f0680762700 Error in foreign key constraint of table laravel/#sql-1f8_19:
 foreign key (`categories_id`) references `categories` (`id`) on delete cascade:
Cannot find an index in the referenced table where the
referenced columns appear as the first columns, or column types
in the table and the referenced table do not match for constraint.
Note that the internal storage type of ENUM and SET changed in
tables created with >= InnoDB-4.1.12, and such columns in old tables
cannot be referenced by such columns in new tables.
Please refer to http://dev.mysql.com/doc/refman/5.7/en/innodb-foreign-key-constraints.html for correct foreign key definition   

【问题讨论】:

  • 错误表明id 上的categories 没有与您给它的约束匹配的关联索引。你也可以分享一下这个迁移吗?

标签: laravel ubuntu migration production


【解决方案1】:

该错误很可能是由于列数据类型不匹配造成的。

id() 创建一个数据类型为unsignedBigInteger 的列

所以如果categories 表的主键由id() 定义,那么brands 表上的外键列应该声明为unsignedBigInteger

外键命名约定应该是被引用表的单数 - category_id 用于在 id 列上引用类别表

尝试如下修改您的迁移

public function up()
{
    Schema::create('brands', function (Blueprint $table) {
        $table->id();
        $table->string('brand_name')->unqiue;
        $table->unsignedBigInteger('category_id');
        $table->boolean('brand_hidden');
        $table->string('brand_color');
        $table->timestamps();

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

【讨论】:

    【解决方案2】:

    感谢它对我有用的反馈:) 我只剩下一个不想合作的人!

            Schema::create('products', function (Blueprint $table) {
                $table->id();
                $table->string('product_name');
                $table->unsignedBigInteger('brand_id');
                $table->unsignedBigInteger('product_image_id')->nullable();
                $table->boolean('product_hidden');
                $table->timestamps();
    
                $table->foreign('brand_id')->references('id')->on('brands')
                    ->onDelete('cascade');
                $table->foreign('product_image_id')->references('id')->on('product_images')
                    ->onDelete('cascade');
            });
    
       
    
    Schema::create('product_images', function (Blueprint $table) {
                $table->id();
                $table->string('product_image_url');
                $table->unsignedBigInteger('product_id');
                $table->timestamps();
                
                $table->foreign('product_id')->references('id')->on('products')
                ->onDelete('cascade');
            });
    

    它给出了这个错误!

     SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table `products` add constraint `products_product_image_id_foreign` foreign key (`product_image_id`) references `product_images` (`id`) on delete cascade)
    

    【讨论】:

      猜你喜欢
      • 2017-06-18
      • 1970-01-01
      • 2018-06-18
      • 1970-01-01
      • 2021-02-08
      • 2018-01-21
      相关资源
      最近更新 更多