【问题标题】:Laravel 5.7 artisan migrateLaravel 5.7 工匠迁移
【发布时间】:2019-04-22 02:02:53
【问题描述】:

我最近安装了 laravel 5.7。如何修复错误“php artisan migrate”?

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 767 bytes (SQL: alter table `users` add unique `users_email_unique`(`email`))

  at C:\laravel\blg2\vendor\laravel\framework\src\Illuminate\Database\Connection.php:664
    660|         // If an exception occurs when attempting to run a query, we'll format the error    661|         // message to include the bindings with SQL, which will make this exception a    662|         // lot more helpful to the developer instead of just the database's errors.    663|         catch (Exception $e) {
  > 664|             throw new QueryException(
    665|                 $query, $this->prepareBindings($bindings), $e
    666|             );
    667|         }
    668|

  Exception trace:

  1   PDOException::("SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes")      C:\laravel\blg2\vendor\laravel\framework\src\Illuminate\Database\Connection.php:458

  2   PDOStatement::execute()
      C:\laravel\blg2\vendor\laravel\framework\src\Illuminate\Database\Connection.php:458

  Please use the argument -v to see more details.

谢谢

【问题讨论】:

    标签: laravel-5.7


    【解决方案1】:

    你也可以定义字符串长度来解决这种错误

    Schema::create('users', function (Blueprint $table) {
                $table->bigIncrements('id');
                $table->string('name',20);
                $table->string('email',80)->unique();
                $table->timestamp('email_verified_at')->nullable();
                $table->string('password',8);
                $table->rememberToken();
                $table->timestamps();
            });
    

    【讨论】:

      【解决方案2】:

      xampp php 7.3.2laravel 5.7 以及 laravel 5.8.3 中也发生了这种情况。只更改了 config/database.php 文件。

      config/database.php
      

      更改了 mysql 上的字符集和排序规则,效果很好。改变这部分

      'charset' => 'utf8mb4',
      'collation' => 'utf8mb4_unicode_ci',
      

      作为

      'charset' => 'utf8',
      'collation' => 'utf8_unicode_ci',
      

      现在它将接受长键。

      【讨论】:

        【解决方案3】:

        打开你的 AppServiceProvider.php,路径 >> App\Providers\AppServiceProvider.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);**
            }
        

        【讨论】:

          【解决方案4】:

          如果您运行的 MySQL 版本早于 5.7.7 版本或 MariaDB 版本早于 10.2.2 版本,您可能需要手动配置迁移生成的默认字符串长度,以便 MySQL 为其创建索引.

          你可以按照 Chin Leung 说的把它修好。

          或者直接在迁移中添加记录的长度。

          $table->string('email', 100);具有可选长度的 VARCHAR 等效列。

          public function up()
          {
              Schema::create('users', function (Blueprint $table) {
                  $table->increments('id');
                  $table->string('name', 50);
                  $table->string('email', 90)->unique();
                  $table->timestamp('email_verified_at')->nullable();
                  $table->string('password');
                  $table->rememberToken();
                  $table->timestamps();
              });
          }
          

          【讨论】:

            【解决方案5】:

            你需要去 App\Providers\AppServiceProvider.php

            现在添加以下代码,然后保存并运行 php artisan serve

            use Illuminate\Support\Facades\Schema;
            
            ...
            
            public function boot()
            {
            Schema::defaultStringLength(191);
            }
            

            我使用 Laravel 5.7,但这个错误总是出现。在每个项目中添加此代码。也尝试将 Laravel 与 php7+ 一起使用,这样它就不会显示PDO::Exception Error...

            【讨论】:

              【解决方案6】:

              Laravel 默认使用 utf8mb4 字符集,其中包括支持在数据库中存储“表情符号”。如果您运行的 MySQL 版本早于 5.7.7 版本或 MariaDB 版本早于 10.2.2 版本,您可能需要手动配置迁移生成的默认字符串长度,以便 MySQL 为其创建索引。

              在您的 AppServiceProvider 中:

              use Illuminate\Support\Facades\Schema;
              
              ...
              
              public function boot()
              {
                  Schema::defaultStringLength(191);
              }
              

              或者,您可以为您的数据库启用 innodb_large_prefix 选项。有关如何正确启用此选项的说明,请参阅您的数据库文档。

              有关索引的更多信息:https://laravel.com/docs/5.7/migrations#indexes

              【讨论】:

              • 非常感谢。
              • 没问题@user2301515 :)
              猜你喜欢
              • 2016-07-08
              • 2018-12-02
              • 1970-01-01
              • 2016-04-06
              • 2014-06-01
              • 2017-11-29
              • 2018-09-16
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多