Framework: Specified key was too long; max key length is 767 bytes

Created on 18 Aug 2017  路  9Comments  路  Source: laravel/framework

  • Laravel Version: 5.5 -- dev
  • PHP Version: PHP 7.1.8-2+ubuntu17.04.1+deb.sury.org+4
  • Database : 10.1.25-MariaDB

Description:

[IlluminateDatabaseQueryException] SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes (SQL: alter table users add uni que users_email_unique(email))

Steps To Reproduce:

1.Create the new app
2.Set Env values
3.Run php artisan make:auth
4.Run php artisan migrate
5.Got Error

Steps To Fix:

1.Goto app/Providers/AppServiceProvider.php

  1. Add \Illuminate\Support\Facades\Schema::defaultStringLength(191); in that boot method

is it really correct fix ?

Most helpful comment

@amenk There is no 'bug', 'fix' per se. It's because the default length is different. Either upgrade your MySQL/MariaDB versions or add the defaultStringLength(191); to boot method of AppServiceProvider.php.

All 9 comments

Yes, check the documentation.

@themsaid thanks

check out https://laravel.com/docs/master/migrations/#creating-indexes

but if possible put some default values :)

10.2 MariaDB is now stable, any reason for not upgrading to fix this issue @kalidasweb?

I am confused .. is this fixed in current laravel versions or not?

@amenk There is no 'bug', 'fix' per se. It's because the default length is different. Either upgrade your MySQL/MariaDB versions or add the defaultStringLength(191); to boot method of AppServiceProvider.php.

I'm getting the same error in a Laravel project even after making various updates. I'm running the following versions on a local ampps server on Windows 10:
Apache 2.4.27
Php 7.1.8
MySql 5.6.35 InnoDB utf8_general_ci collation (changed from utf8mb4_general_ci)
Laravel 5.6.39

In AppServiceProvider.php near the top I've added:
use IlluminateSupport|FacadesSchema;

And in function boot I've added:
Schema::defaultStringLength(191);

In config/database.php I've changed charset tp 'utf8',collation to 'utf8_general_ci', and engine to 'InnoDB ROW_FORMAT=DYNAMIC'.

Using php artisan migrate:fresh I even still get the error if in the xyz_create_users_table.php I change $table->string('email')->inique(); to $table->string('email');. The first error says:

聽 1聽聽 DoctrineDBALDriverPDOException::("SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes")
vendordoctrinedballibDoctrineDBALDriverPDOStatement.php:144

I was wondering if the doctrine cache needed clearing but php app/console cache:clear said "could not open input file".

I'm getting the same error in a Laravel project even after making various updates. I'm running the following versions on a local xampp server on Windows 10:
Apache 2.4.34
PHP 7.2.8
MariaDB 10.1.34
Laravel 5.7.26

IlluminateDatabaseQueryException : SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'users' already exists (SQL: create table users (id int unsigned not null auto_increment primary key, name varchar(255) not null, email varchar(255) not null, email_verified_at timestamp null, password varchar(255) not null, remember_token varchar(100) null, created_at timestamp null, updated_at timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci')

the problem exists to the version 5.4 laravel why no one will change so that you do not have to change every time you create a new project?

The exception that you pasted is not the same as the exception at issue here.

Need at least Maria 10.2.2. I would think that any "fixes" that the framework could provide, so that this works even when using old database versions, would be a huge bc break dealing with migrations.

Was this page helpful?
0 / 5 - 0 ratings