Laravel5的数据库表建立问题 数据库迁移操作报错问题解决


执行迁移文件: php artisan migrate ; 数据库中就会看到如下报错:
Migration table created successfully.
In Connection.php line 647:
SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was t
oo long; max key length is 767 bytes (SQL: alter table `users` add unique `
users_email_unique`(`email`))
In Connection.php line 449:
SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was t
oo long; max key length is 767 bytes
这是由于Laravel 默认使用 utf8mb4 字符, 包括支持在数据库存储「 表情」 。 如果你正在运行的 MySQL release 版本低于5.7.7 或 MariaDB release
版本低于10.2.2 , 为了MySQL为它们创建索引, 你可能需要手动配置迁移生成的默认字符串长度, 你可以通过调用 AppServiceProvider 中的
Schema::defaultStringLength 方法来配置它:\
解决办法, app/Providers/AppServiceProvider .php
<?php namespace App\Providers; use Illuminate\Support\ServiceProvider; use Illuminate\Support\Facades\Schema; //添加的代码 class AppServiceProvider extends ServiceProvider { /** * Bootstrap any application services. * * @return void */ public function boot() { Schema::defaultStringLength(191);//添加的代码 } /** * Register any application services. * * @return void */ public function register() { // } }
再次执行迁移文件: php artisan migrate ; 就可以成功创建表了
Migration table created successfully. Migrating: 2014_10_12_000000_create_users_table Migrated: 2014_10_12_000000_create_users_table Migrating: 2014_10_12_100000_create_password_resets_table Migrated: 2014_10_12_100000_create_password_resets_table Migrating: 2019_05_15_072207_create_articles_table Migrated: 2019_05_15_072207_create_articles_table
