我遇到了laravel迁移的问题,我需要为特定列设置索引长度,但看起来Schema/Blueprint index()没有这样的功能.Laravel文件
[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`))
Run Code Online (Sandbox Code Playgroud)
原始的sql查询行:
KEY `description_idx` (`description`(100)) // index length = 100
Run Code Online (Sandbox Code Playgroud)
Laravel迁移代码行:
$table->text('description')->nullable()->index('`description_idx`'); // no index length here
Run Code Online (Sandbox Code Playgroud)
我觉得我能做的最好的事情就是改变列类型,但也许有更合适的方法来解决这个问题?
您可以使用DB :: statement()手动创建这些索引; 在您的迁移中.
在你的up()做这样的事情
DB::statement('CREATE INDEX description_idx ON Customers (description(100));');
Run Code Online (Sandbox Code Playgroud)
然后在你的down()你可以简单
Schema::table('Customers', function($table) {
$table->dropIndex('description_idx');
});
Run Code Online (Sandbox Code Playgroud)
希望有所帮助.
| 归档时间: |
|
| 查看次数: |
1768 次 |
| 最近记录: |