代码之家  ›  专栏  ›  技术社区  ›  TheWebs

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

  •  0
  • TheWebs  · 技术社区  · 6 年前

    请考虑以下示例:

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

    然后抛出错误:

    SQLSTATE[42000]:语法错误或访问冲突:1059标识符 名称 “lsd_location_units_site_id_inventory_items_lsd_location_id_unique”是 太长(SQL:alter table lsd_location_units 添加唯一的 lsd_location_units_site_id_inventory_items_lsd_location_id_unique ( site_id , inventory_items , lsd_location_id ))

    所以问题是:我能给它取一个不同的名字,并且保持它原来的唯一键吗,所以也许不是它的长名字: sid_ii_lsd_location_unuiqe 或者类似的?

    (一) 在MYSQL中允许这样做吗?

    (二) 有办法解决这个问题吗?因为我需要这三样东西。

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

    1 回复  |  直到 6 年前
        1
  •  1
  •   Seva Kalashnikov    6 年前

    第二个论点 unique 是索引键名称,请尝试:

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

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

    MySQL 5.6中的最大索引名称长度为64个字符( ref )

    推荐文章