Laravel版本:v7.25.0
故障报错的截图:
故障报错的描述:
D:\phpstudy_pro\WWW\test002> php artisan migrate
Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table
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 D:\phpstudy_pro\WWW\test002\vendor\laravel\framework\src\Illuminate\Database\Connection.php:671
667| // If an exception occurs when attempting to run a query, we'll format the error
668| // message to include the bindings with SQL, which will make this exception a
669| // lot more helpful to the developer instead of just the database's errors.
670| catch (Exception $e) {
> 671| throw new QueryException(
672| $query, $this->prepareBindings($bindings), $e
673| );
674| }
675|
1 D:\phpstudy_pro\WWW\test002\vendor\laravel\framework\src\Illuminate\Database\Connection.php:464
PDOException::("SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 1000 bytes")
2 D:\phpstudy_pro\WWW\test002\vendor\laravel\framework\src\Illuminate\Database\Connection.php:464
PDOStatement::execute()
原因分析:
搜索该错误,首先尝试了这个方法:MySQL版本低于5.7.7,所以会有这个报错,但实际我更换为MySQL 8后依然存在同样故障报错。
排除这个原因,按报错信息来理解,什么值超过限制的最大值,所以尝试了以下方法,得以解决。
正确方法:修改 \app\Providers\AppServiceProvider.php 文件增加下方标记的2行代码
<?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); //增加这一行给 boot方法 增加一个默认值
}
}
保存后,再次执行 php artisan migrate ,成功执行迁移操作!
修改完成重新执行迁移命令之前,需要将上一次错误执行时添加的部分数据表删除,否则会报数据表冲突的错误