【问题标题】:Laravel : How can I change default indexing key length in morph tableLaravel:如何更改变形表中的默认索引键长度
【发布时间】:2026-02-07 22:30:01
【问题描述】:

我有表“invoice_relations”,它是变形表。 所以在迁移中我写道:

$table->morphs('invoice_relations');

但是在运行迁移时会出错,

语法错误或访问冲突:1059 标识符 na
我的“invoice_relations_invoice_relations_id_invoice_relations_type_index”在 /var/www/html/st/sales-tantra/vendor/doctrine/dbal/ 中太长了
lib/Doctrine/DBAL/Driver/PDOStatement.php:105

【问题讨论】:

    标签: mysql laravel database-migration


    【解决方案1】:

    改变你的

    $table->morphs('invoice_relations');
    

    到这里:

    $table->morphs('invoice_relations', 'invoice_relations_morpf_key');
    

    或者这个:

        $table->unsignedInteger("invoice_relations_id");
        $table->string("invoice_relations_type");
        $table->index(["invoice_relations_id", "invoice_relations_type"], "YOUR_INDEX_NAME");
    

    但我认为多态关系名称的名称以“能”结尾,例如可关联的。

    https://laravel.com/docs/5.6/eloquent-relationships#polymorphic-relations

    【讨论】:

    • 谢谢 J. Doe。 $table->morphs('invoice_relations', 'invoice_relationable_morph_key');正在为我工​​作。