在做多个唯一列时,我可以在 laravel 5.6 中给出自定义名称吗?

When doing multiple unique columns can I give a custom name in laravel 5.6?

考虑以下示例:

$table->unique(['site_id', 'inventory_items', 'lsd_location_id']);

然后抛出错误:

SQLSTATE[42000]: Syntax error or access violation: 1059 Identifier name 'lsd_location_units_site_id_inventory_items_lsd_location_id_unique' is too long (SQL: alter table lsd_location_units add unique lsd_location_units_site_id_inventory_items_lsd_location_id_unique(site_id, inventory_items, lsd_location_id))

所以问题是:我可以给它一个不同的名称并仍然保留唯一的密钥,所以而不是它的长名称可能:sid_ii_lsd_location_unuiqe 或类似的东西?

A) MYSQL 允许吗?

B) 有办法解决这个问题吗?因为这三个我都需要。

目标是每个站点每个 LSD 位置 ID 只有一个 inventory_item_id。

unique的第二个参数是索引键名,试试:

$table->unique(['site_id', 'inventory_items', 'lsd_location_id'], 'sid_ii_lsd_location_unuiqe');

来自 laravel docs: Laravel will automatically generate a reasonable index name, but you may pass a second argument to the method to specify the name yourself

MySQL5.6 中的最大索引名称长度为 64 个字符 (ref)