问题:运行迁移文件报错
Illuminate\Database\QueryException
SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 1000 bytes (SQL: alter table `users` add unique `users_email_unique`(`email`))
at E:\phpstudy_pro\WWW\shopapi\vendor\laravel\framework\src\Illuminate\Database\Connection.php:712
708▕ // If an exception occurs when attempting to run a query, we'll format the error
709▕ // message to include the bindings with SQL, which will make this exception a
710▕ // lot more helpful to the developer instead of just the database's errors.
711▕ catch (Exception $e) {
➜ 712▕ throw new QueryException(
713▕ $query, $this->prepareBindings($bindings), $e
714▕ );
715▕ }
716▕ }
1 E:\phpstudy_pro\WWW\shopapi\vendor\laravel\framework\src\Illuminate\Database\Connection.php:501
PDOException::("SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 1000 bytes")
2 E:\phpstudy_pro\WWW\shopapi\vendor\laravel\framework\src\Illuminate\Database\Connection.php:501
PDOStatement::execute()
这是由于Laravel 默认使用 utf8mb4 字符, 包括支持在数据库存储「 表情」 。 如果你正在运行的 MySQL release 版本低于5.7.7 或 MariaDB release
版本低于10.2.2 , 为了MySQL为它们创建索引, 你可能需要手动配置迁移生成的默认字符串长度, 你可以通过调用 AppServiceProvider 中的
Schema::defaultStringLength 方法来配置它:
解决办法
1. E:\phpstudy_pro\WWW\shopapi\app\Providers\AppServiceProvider.php
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Schema;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
//
Schema::defaultStringLength(191);
}
}
这两句代码 加上
use Illuminate\Support\Facades\Schema;
public function boot()
{
//
Schema::defaultStringLength(191);
}
再次执行迁移文件:
php artisan migrate