user2301515
user2301515

Reputation: 5117

Laravel 5.7 artisan migrate

I installed recently laravel 5.7. How to fix error "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.

Thank you

Upvotes: 0

Views: 3182

Answers (6)

Rasel Rana
Rasel Rana

Reputation: 1

You can define the string length as well to solve this kind of error

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

Upvotes: 0

Motahar Hossain
Motahar Hossain

Reputation: 563

On xampp php 7.3.2, laravel 5.7 & also in laravel 5.8.3 it happened to me. Only changed the config/database.php file.

config/database.php

Changed charset & collation on mysql & it worked fine. Change this part

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

as

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

Now it will accept the long keys.

Upvotes: 1

Ravinesh
Ravinesh

Reputation: 129

Open your 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);**
    }

Upvotes: 0

Edgar Tek
Edgar Tek

Reputation: 29

If you are running a version of MySQL older than the 5.7.7 release or MariaDB older than the 10.2.2 release, you may need to manually configure the default string length generated by migrations in order for MySQL to create indexes for them.

You can have it fixed as Chin Leung said.

Or adding the length of the record directly in the migration.

$table->string('email', 100); VARCHAR equivalent column with a optional length.

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

Upvotes: 1

vinayak shahdeo
vinayak shahdeo

Reputation: 1488

You need to go to App\Providers\AppServiceProvider.php

Now Add the following code and then save and run php artisan serve

use Illuminate\Support\Facades\Schema;

...

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

I use Laravel 5.7 but this error always comes up. ADD this code in every project. Also try to use Laravel with php7+ so that it doesnot show you PDO::Exception Error...

Upvotes: 0

Chin Leung
Chin Leung

Reputation: 14941

Laravel uses the utf8mb4 character set by default, which includes support for storing "emojis" in the database. If you are running a version of MySQL older than the 5.7.7 release or MariaDB older than the 10.2.2 release, you may need to manually configure the default string length generated by migrations in order for MySQL to create indexes for them.

Inside your AppServiceProvider:

use Illuminate\Support\Facades\Schema;

...

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

Alternatively, you may enable the innodb_large_prefix option for your database. Refer to your database's documentation for instructions on how to properly enable this option.

For more information about indexes: https://laravel.com/docs/5.7/migrations#indexes

Upvotes: 3

Related Questions