原文地址: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'
出現這兩種情況,說明你的遷移檔案應該有問題,第一種,說明遷移檔案欄位應該是重復了,第二個說明,資料庫中已有資料表,請先洗掉表后,在進行遷移,
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/6576.html
標籤:PHP

