Summer Sale! All accounts are 50% off this week.

Xupha's avatar
Level 1

Laravel 5.6.9 : php artisan migrate - problem

Hi, I'm new for Laravel. I installed Laravel and having trouble to migrate new table call tasks. My local env is running XAMPP on Window. The error is

   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:\xampp\htdocs\blog\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:\xampp\htdocs\blog\vendor\laravel\framework\src\Illuminate\Database\Connection.php : 458

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

  Please use the argument -v to see more details.

How can I solve these issue?

0 likes
9 replies
takdw's avatar

According to the Laravel Docs this problem occurs if you're using MariaDB or MySQL versions older than 5.7.7.

To fix the error, go to app/Providers/AppServiceProvider.php and add a boot function as follows. (Make sure to import the Schema facade from Illuminate\Support\Facades\Schema.

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Schema;


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

The docs also say that you can enable innodb_large_prefix option in your database and the probelm should be fixed. If you're using MySQL I suggest you to upgrade to the latest version and not bother with any of the above.

2 likes
Xupha's avatar
Level 1

Hi guys, I upgrade MariaDB to 10.2.13 It's working for Laravel version 5.4 but still facing problem for Laravel 5.6.9 as below.

   Illuminate\Database\QueryException  : SQLSTATE[42000]: Syntax error or access violation: 1170 BLOB/TE
XT column 'email' used in key specification without a key length (SQL: alter table `users` add unique `u
sers_email_unique`(`email`))

  at C:\xampp\htdocs\blog\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: 1170 BLOB/TEXT column 'email' u
sed in key specification without a key length")
      C:\xampp\htdocs\blog\vendor\laravel\framework\src\Illuminate\Database\Connection.php : 458

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

  Please use the argument -v to see more details.

takdw's avatar

Are you trying to use a TEXT or BLOB type column as a primary key?

Edit: I assume that you have assigned the BLOB or TEXT data type to your email column. Try changing the column type to VARCHAR(x). x is the maximum number of characters allowed. Laravel's users table uses a VARCHAR(255) for the email column.

aondoe's avatar

Thank you takdw your solution worked for me.

faizannoor's avatar

File: config/database.php

change the following lines

FROM ->

'charset' => 'utf8mb4',

'collation' => 'utf8mb4_unicode_ci',

TO ->

'charset' => 'utf8',

'collation' => 'utf8_unicode_ci',

2 likes
Cronix's avatar

@faizannoor @oladele Just realize you can't store all true UTF8 data (like emojis) with mysql's UTF8 charset. It isn't a full implementation of UTF8. utf8mb4 fixes that. Mysql's UTF8 only stores 3 bytes/character, while utf8mb4 is true UTF8 and stores 4 bytes/character, which things (like emojis/unicode) and other languages require.

obink's avatar

IDK what's good and bad after I changed this. But it works, and that's it! The next problem is the next problem. Thanks @faizannoor

Please or to participate in this conversation.