gpt4 book ai didi

laravel - Ubuntu laravel 生产迁移不起作用

转载 作者:行者123 更新时间:2023-12-04 18:56:54 27 4
gpt4 key购买 nike

迁移在我的本地服务器上运行良好,但在我的生产服务器(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()创建数据类型为 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');
});
}

关于laravel - Ubuntu laravel 生产迁移不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65156718/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com