Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes的解决方式

laravel8.* / 648人浏览 / 0人评论

Laravel 8.*对默认数据库字符集进行了更改,现在utf8mb4它包含了对存储表情符号的支持。这只会影响新的应用程序,只要您运行MySQL v5.7.7及更高版本,就不需要做任何事情。

数据库迁移创建对应的数据库表结构:

$ php artisan migrate

Laravel 8.*对默认数据库字符集进行了更改,现在utf8mb4它包含了对存储表情符号的支持。这只会影响新的应用程序,只要您运行MySQL v5.7.7及更高版本,就不需要做任何事情。

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_e
mail_unique`(`email`))

  at I:\phpstudy_pro\WWW\laravel-shop\vendor\laravel\framework\src\Illuminate\Da
tabase\Connection.php:685
    681▕         // If an exception occurs when attempting to run a query, we'll
 format the error
    682▕         // message to include the bindings with SQL, which will make th
is exception a
    683▕         // lot more helpful to the developer instead of just the databa
se's errors.
    684▕         catch (Exception $e) {
  ➜ 685▕             throw new QueryException(
    686▕                 $query, $this->prepareBindings($bindings), $e
    687▕             );
    688▕         }
    689▕

  1   I:\phpstudy_pro\WWW\laravel-shop\vendor\laravel\framework\src\Illuminate\D
atabase\Connection.php:478
      PDOException::("SQLSTATE[42000]: Syntax error or access violation: 1071 Sp
ecified key was too long; max key length is 1000 bytes")

  2   I:\phpstudy_pro\WWW\laravel-shop\vendor\laravel\framework\src\Illuminate\D
atabase\Connection.php:478
      PDOStatement::execute()

Laravle8.*迁移指南讲述,您只需编辑 AppServiceProvider.php文件并在 boot方法内设置默认字符串长度:

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

转载注明:

扩展查找

0 条评论

还没有人发表评论

发表评论 取消回复

记住我的信息,方便下次评论
有人回复时邮件通知我