在首次执行 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 E:\MyLaravel\vendor\laravel\framework\src\Illuminate\Database\Connection.php:703
699▕ // If an exception occurs when attempting to run a query, we'll format the error
700▕ // message to include the bindings with SQL, which will make this exception a
701▕ // lot more helpful to the developer instead of just the database's errors.
702▕ catch (Exception $e) {
➜ 703▕ throw new QueryException(
704▕ $query, $this->prepareBindings($bindings), $e
705▕ );
706▕ }
707▕ }
1 E:\MyLaravel\vendor\laravel\framework\src\Illuminate\Database\Connection.php:492
PDOException::("SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 1000 bytes")
2 E:\MyLaravel\vendor\laravel\framework\src\Illuminate\Database\Connection.php:492
PDOStatement::execute()报错原因:
密钥的长度过长。
如果你正在运行的 MySQL release 版本低于5.7.7 或 MariaDB release 版本低于10.2.2 ,为了MySQL为它们创建索引,你可能需要手动配置迁移生成的默认字符串长度,你可以通过调用 AppServiceProvider 中的 Schema::defaultStringLength 方法来配置它。
报错的问题就是max key length too long
那么现在只需要在原本的users migrations代码中添加这一行
public function up()
{
Schema::defaultStringLength(191);//手动控制默认字符串长度
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}或者也可以找到App\Providers\AppServiceProvider.php 文件里,编辑 boot() 内设置默认字符串长度:
use Illuminate\Support\Facades\Schema;
public function boot()
{
Schema::defaultStringLength(191);
}之后删除掉报错时生成的数据表
再执行一次 php artisan migrate 指令 就能正常创建了

