表结构迁移文件Identifier name 'xxxxxx' is too long解决
问题描述
使用表结构生成工具自动生成迁移文件我用的扩展包是 kitloong/laravel-migrations-generator
使用命令根据数据库结构生成迁移文件
php artisan migrate:generate
在迁移的时候报错标识符名称太长
SQLSTATE[42000]: Syntax error or access violation: 1059 Identifier name 'circle_collections_subscribe_circle_collection_id_member_id_primary' is too long (SQL: alter table `circle_collections_subscribe` add primary key `circle_collections_subscribe_circle_collection_id_member_id_primary`(`circle_collection_id`, `member_id`))
由于没有自定义标识符的名称加上表名称又太长,拼接后的名称这么长确实有点过分
circle_collections_subscribe_circle_collection_id_member_id_primary
解决方法
参考Laravel migration primary (or key) “Identifier name is too long”
- 找到报错的迁移文件
public function up() { Schema::create('circle_collections_subscribe', function (Blueprint $table) { $table->unsignedInteger('circle_collection_id'); $table->unsignedInteger('member_id')->index('circle_collections_subscribe_member_id'); $table->primary(['circle_collection_id', 'member_id']); }); }
- 由于这边没有定义标识符的名称所以会自动拼接,名称过长导致迁移失败。
$table->primary(['circle_collection_id', 'member_id']);
- 只需要在里面自定义个短一点的名称就可以了
$table->primary(['circle_collection_id', 'member_id'],"circle_collections_subscribe_primary");
如何避免
表名称尽量能短一点就短一点吧,有时候短一点也是有好处的
本作品采用《CC 协议》,转载必须注明作者和本文链接
推荐文章: