Laravel 5.7工匠迁移

时间:2018-11-19 16:49:50

标签: laravel-5.7

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

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.

谢谢

6 个答案:

答案 0 :(得分:3)

  

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

答案 1 :(得分:1)

  

如果运行的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();
    });
}

答案 2 :(得分:0)

您需要转到  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...

答案 3 :(得分:0)

打开您的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 :(得分:0)

xampp php 7.3.2 laravel 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',

现在它将接受长键。

答案 5 :(得分:0)

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

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