legend2v2---6、SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long legend2v2---6、SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long

一、总结

一句话总结:

you need to place this code in your 【AppServiceProvider.php】:use IlluminateSupportFacadesSchema;  public function boot() {     【Schema::defaultStringLength(191);】 }

二、SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long

1、问题

运行
php artisan migrate
出现
SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 1000 bytes (SQL: alter table `users` add u
nique `users_email_unique`(`email`))
 
legend2v2---6、SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long
legend2v2---6、SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long

2、解答及代码

网上有很多很多资料,比如可以参照下列博客:

https://www.cnblogs.com/linzenews/p/12764939.html

https://blog.csdn.net/sqlquan/article/details/81153777

https://blog.csdn.net/sinat_33801009/article/details/80817486

等等等等

If you are using MariaDB or an older version of MySQL, you need to place this code in your AppServiceProvider.php:

use IlluminateSupportFacadesSchema;  public function boot() {     Schema::defaultStringLength(191); } 

legend2v2---6、SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long
legend2v2---6、SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long

<?php

namespace AppProviders;

use IlluminateSupportServiceProvider;
use IlluminateSupportFacadesSchema;

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);
    }
}

这样做就成功解决问题:

legend2v2---6、SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long
legend2v2---6、SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long