我在 Laravel 迁移中遇到了一个问题,我需要为特定列设置索引长度,但看起来 Schema/Blueprint index() 没有这样的功能。 Laravel Docs
[Illuminate\Database\QueryException]
SQLSTATE[42000]: Syntax error or access violation:
1170 BLOB/TEXT column 'description' used in key specification
without a key length (SQL: alter table `Customers`
add index `description_idx`(`description`))
原始sql查询行:
KEY `description_idx` (`description`(100)) // index length = 100
Laravel 迁移代码行:
$table->text('description')->nullable()->index('`description_idx`'); // no index length here
目前我觉得我能做的最好的事情就是更改列类型,但也许有更合适的方法来解决这个问题?
最佳答案
您可以使用 DB::statement() 手动创建这些索引;在您的迁移中。
在你的 up() 中做这样的事情
DB::statement('CREATE INDEX description_idx ON Customers (description(100));');
然后在你的 down() 中你可以简单地
Schema::table('Customers', function($table) {
$table->dropIndex('description_idx');
});
希望有帮助。
关于Laravel 4 迁移模式 - 索引长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24792974/