php laravel 迁移中外来键索引名称大小错误

问题描述 投票:0回答:3

我在迁移过程中遇到错误,如下所述:

SQLSTATE[42000]: Syntax error or access violation: 1059 Identifier name 'port_call_departure_time_readings_port_call_departure_time_condition_id_foreign' is too long (SQL: alter table port_call_departure_time_readings add index port_call_departure_time_readings_port_call_departure_time_condition_id_foreign(port_call_departure_time_condition_id)):

FK 索引名称允许的大小为 59,我在迁移过程中对此进行了测试。 但原来的FK索引名称大小是79。

您能否向我解释一下在 port_call_departure_time_readings 表中为 port_call_departure_time_condition_id 字段分配 FK 索引名称的最佳解决方案是什么?

php laravel migration size
3个回答
4
投票

试试这个

$table->index('column','index_name')


1
投票

foreign()
方法接受索引名称作为其第二个参数。例如:

$table->foreign('user_id', 'your_index_name_foreign')
      ->references('id')->on('users');

当您不指定名称时,Laravel 默认连接表名和列名。在某些情况下,默认名称比数据库允许的索引长。


0
投票

根据 Laravel 11 中的文档;

$table->foreignId('foreign_id')->constrained(indexName: 'new_index_name')->references('id')->on('foreign_table_name');
© www.soinside.com 2019 - 2024. All rights reserved.