Reputation: 359
I got this issue when I use laravel migration to create a table in the database. I don't know what might be wrong with it as I use the same command on my previous system. This is the error I got while using composer to create the table.
$ php artisan migrate Migration table created successfully.
[Illuminate\Database\QueryException] SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was t oo long; max key length is 767 bytes (SQL: alter table
users
add uniqueusers_email_unique
([PDOException] SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was t oo long; max key length is 767 bytes
Upvotes: 0
Views: 877
Reputation: 1103
The character limit depends on the character set you use. Laravel
is using utf8mb4
for which the character limit of the largest index is 191
.
You just need to set your character limit to correct one. Read this from Laravel Index Length
Index Lengths & MySQL / MariaDB
Laravel uses the
utf8mb4
character set by default, which includes support for storing "emojis" in the database. If you are running a version ofMySQL
older than the5.7.7
release orMariaDB
older than the10.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 may configure this by calling theSchema::defaultStringLength
method within yourAppServiceProvider
:
use Illuminate\Support\Facades\Schema;
/**
* Bootstrap any application services.
*
* @return void
*/
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.
Upvotes: 2
Reputation: 5186
Laravel 5.4 uses utf8mb4
by default. One way to fix this error, is to default back to utf8
.
Go to your config\database.php
and change the database charset
to uft8
and the collation
to utf8_unicode_ci
:
'mysql' => [
//..
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
//..
],
Upvotes: 4
Reputation: 900
It's a bug in Laravel 5.4
To fix it, place this above the Schema::create method call:
Schema::defaultStringLength(191);
Read more here:
https://github.com/laravel/framework/issues/17508
Upvotes: 1