原文地址:https://www.wjcms.net/archives/laravel%E6%95%B0%E6%8D%AE%E5%BA%93%E8%BF%81%E7%A7%BB%E6%97%B6%E6%8A%A5%E9%94%99 問題描述 [Illuminate\Database\QueryExc ...
原文地址:https://www.wjcms.net/archives/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'
出現這兩種情況,說明你的遷移文件應該有問題,第一種,說明遷移文件欄位應該是重覆了,第二個說明,資料庫中已有數據表,請先刪除表後,在進行遷移。