Laravel 5.7工匠迁移

问题描述 投票:0回答:5

我最近安装了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
5个回答
3
投票

Laravel默认使用utf8mb4字符集,其中包括支持在数据库中存储“emojis”。如果您运行的是早于5.7.7版本的MySQL版本或早于10.2.2版本的MariaDB,您可能需要手动配置迁移生成的默认字符串长度,以便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
投票

如果您运行的是早于5.7.7版本的MySQL版本或早于10.2.2版本的MariaDB,您可能需要手动配置迁移生成的默认字符串长度,以便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();
    });
}

0
投票

你需要去App\Providers\AppServiceProvider.php

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

use Illuminate\Support\Facades\Schema;

...

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

我使用Laravel 5.7但总会出现这个错误。在每个项目中添加此代码。同时尝试使用带有php7 +的Laravel,这样它就不会向你显示PDO::Exception Error...


0
投票

打开AppServiceProvider.php,Path >> 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);**
    }

0
投票

在xampp php 7.3.2,laravel 5.7以及laravel 5.8.3中,它发生在我身上。仅更改了config / database.php文件。

config/database.php

在mysql上更改charset和collat​​ion,它运行正常。改变这一部分

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

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

现在它将接受长按键。

© www.soinside.com 2019 - 2024. All rights reserved.