Foregin键索引名称php laravel迁移中的大小错误

时间:2018-11-09 10:54:42

标签: php laravel migration size

在迁移过程中出现错误,如下所述:

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_condition_id字段在port_call_departure_time_readings表中分配FK索引名称的最佳解决方案是什么?

2 个答案:

答案 0 :(得分:3)

尝试一下$table->index('column','index_name')

答案 1 :(得分:0)

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

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

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