Laravel Migration Error: Syntax error or access vi

2019-01-04 23:18发布

Migration error on Laravel 5.4 with php artisan make:auth

[Illuminate\Database\QueryException] SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes (SQL: alter tabl e users add unique users_email_unique(email))

[PDOException] SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes

23条回答
ら.Afraid
2楼-- · 2019-01-04 23:27

If you want to change in AppServiceProvider then you need to define the length of email field in migration. just replace the first line of code to the second line.

create_users_table

$table->string('email')->unique();
$table->string('email', 50)->unique();

create_password_resets_table

$table->string('email')->index();
$table->string('email', 50)->index();

After successfully changes you can run the migration.
Note: first you have to delete (if you have) users table, password_resets table from the database and delete users and password_resets entries from migration table.

查看更多
放荡不羁爱自由
3楼-- · 2019-01-04 23:27

This is common since Laravel 5.4 changed the default database charater set to utf8mb4. What you have to do, is: edit your App\Providers.php by putting this code before the class declaration

use Illuminate\Support\Facades\Schema;

Also, add this to the 'boot' function Schema::defaultStringLength(191);

查看更多
ゆ 、 Hurt°
4楼-- · 2019-01-04 23:28

Schema::defaultStringLength(191); will define the length of all strings 191 by default which may ruin your database. You must not go this way.

Just define the length of any specific column in the database migration class. For example, I'm defining the "name", "username" and "email" in the CreateUsersTable class as below:

public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name', 191);
            $table->string('username', 30)->unique();
            $table->string('email', 191)->unique();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }
查看更多
Fickle 薄情
5楼-- · 2019-01-04 23:34

update & insert these lines in app/Providers/AppServiceProvider.php

use Illuminate\Support\Facades\Schema;  //insert this line
public function boot()
{
    Schema::defaultStringLength(191); //insert this line also
}

set your database engine in config/database.php in 'mysql' array

'engine' => 'InnoDB',
查看更多
Rolldiameter
6楼-- · 2019-01-04 23:35

Instead of setting a limit on length I would propose the following, which has worked for me.

Inside

config/database.php

replace this line for mysql

'engine' => 'InnoDB ROW_FORMAT=DYNAMIC',

upon

'engine' => null,
查看更多
Ridiculous、
7楼-- · 2019-01-04 23:37

I have solved this issue and edited my config->database.php file to like my database ('charset'=>'utf8') and the ('collation'=>'utf8_general_ci'), so my problem is solved the code as follow:

'mysql' => [
        'driver' => 'mysql',
        'host' => env('DB_HOST', '127.0.0.1'),
        'port' => env('DB_PORT', '3306'),
        'database' => env('DB_DATABASE', 'forge'),
        'username' => env('DB_USERNAME', 'forge'),
        'password' => env('DB_PASSWORD', ''),
        'unix_socket' => env('DB_SOCKET', ''),
        'charset' => 'utf8',
        'collation' => 'utf8_general_ci',
        'prefix' => '',
        'strict' => true,
        'engine' => null,
    ],
查看更多
登录 后发表回答