共計 1295 個字符,預計需要花費 4 分鐘才能閱讀完成。
自動寫代碼機器人,免費開通
這期內容當中丸趣 TV 小編將會給大家帶來有關 laravel 中數據庫遷移時報錯如何解決,文章內容豐富且以專業的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
問題描述
[Illuminate\Database\QueryException]
SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes (SQL:
alter table `users` add unique `users_email_unique`(`email`))
[PDOException]
SQLSTATE[42000]: Syntax
error or access violation:
1071 Specified
key was too
long; max key length is 767 bytes
(1)laravel 5.4 或者更高版本 改變了默認的數據庫字符集,現在 utf8mb4 包括存儲 emojis 支持。如果你運行 MySQL v5.7.7 或者更高版本,則不需要做任何事情。(2)當你試著在一些 MariaDB 或者一些老版本的的 MySQL 上運行 migrations 命令時,你可能會碰到下面這個錯誤:Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes【指定的鍵太長了,最大鍵的長是 767bytes,因為 laravel 默認字符串長度是 767bytes,所以要自己去手動配置。】
解決方案:
?php
namespace App\Providers;
// 導入 Schema 類
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
public
function boot()
{
// 在 app/providers/AppServiceProvider.php 中 boot 方法中加上
Schema::defaultStringLength(191);
}
}
另外還有一種錯誤
SQLSTATE[42S21]: Column already
exists:
1060 Duplicate column name
description
或者是
SQLSTATE[42S21]: Table already
exists:
1060 name
articles
上述就是丸趣 TV 小編為大家分享的 laravel 中數據庫遷移時報錯如何解決了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注丸趣 TV 行業資訊頻道。
向 AI 問一下細節
正文完