mysql 5.5和5.6 执行数据库迁移报错问题
执行 php artisan migrate
时报错:
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 K:\temp\000\blog\vendor\laravel\framework\src\Illuminate\Database\Connection.php:669
665| // If an exception occurs when attempting to run a query, we'll format the error
666| // message to include the bindings with SQL, which will make this exception a
667| // lot more helpful to the developer instead of just the database's errors.
668| catch (Exception $e) {
> 669| throw new QueryException(
670| $query, $this->prepareBindings($bindings), $e
671| );
672| }
673|
Exception trace:
1 PDOException::("SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 1000 bytes")
K:\temp\000\blog\vendor\laravel\framework\src\Illuminate\Database\Connection.php:463
2 PDOStatement::execute()
K:\temp\000\blog\vendor\laravel\framework\src\Illuminate\Database\Connection.php:463
Please use the argument -v to see more details.
原因
框架自带的迁移文件 2014_10_12_000000_create_users_table
执行迁移的时候有个字段 email
没有指定varchar长度,所以默认长度会是 255,然而执行创建索引 users_email_unique
时不允许这么长
解决
修改文件 app\Providers\AppServiceProvider.php
内容如下:
use Illuminate\Support\Facades\Schema;
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
Schema::defaultStringLength(150);
}
}