使用Laravel進(jìn)行數(shù)據(jù)庫(kù)遷移文件開(kāi)發(fā),有些字段我們需要建立索引,
$table->string('username')->unique();
當(dāng)創(chuàng)建完索引,并使用
php artisan migrate:refresh
重建,發(fā)現(xiàn)提示錯(cuò)誤信息如下:
[Illuminate\Database\QueryException]
SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 1000 bytes (S
QL: alter table `users` add unique `users_username_unique`(`username`))
[PDOException]
SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 1000 bytes
查看報(bào)錯(cuò)信息我們發(fā)現(xiàn)是由于字段定義的長(zhǎng)度導(dǎo)致,
修改代碼,對(duì)該索引字段進(jìn)行長(zhǎng)度定義:
$table->string('username', 60)->unique()->comment('用戶名');
重新運(yùn)行遷移指令,錯(cuò)誤得到解決,同時(shí)我們也創(chuàng)鍵了索引,截圖如下:
$table->string('username')->unique();
當(dāng)創(chuàng)建完索引,并使用
php artisan migrate:refresh
重建,發(fā)現(xiàn)提示錯(cuò)誤信息如下:
[Illuminate\Database\QueryException]
SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 1000 bytes (S
QL: alter table `users` add unique `users_username_unique`(`username`))
[PDOException]
SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 1000 bytes
查看報(bào)錯(cuò)信息我們發(fā)現(xiàn)是由于字段定義的長(zhǎng)度導(dǎo)致,
修改代碼,對(duì)該索引字段進(jìn)行長(zhǎng)度定義:
$table->string('username', 60)->unique()->comment('用戶名');
重新運(yùn)行遷移指令,錯(cuò)誤得到解決,同時(shí)我們也創(chuàng)鍵了索引,截圖如下: