davy_yg's avatar
Level 27

Specified key was too long; max key length is 1000 bytes (SQL: alter table `users` add unique `users_email_unique`(`email`))

[orderbli@dci17 staging.orderblind.com]$ php artisan migrate 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 1000 bytes (SQL: alter table users add unique users_email_unique(email))

migrations\2014_10_12_000000_create_users_table.php

	public function up()
	{
    Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('email')->unique();
        $table->string('password');
        $table->string('is_admin');
        $table->rememberToken();
        $table->timestamps();
    });
	}

AppServiceProvider.php

		<?php

	namespace App\Providers;

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

	class AppServiceProvider extends ServiceProvider
	{
	/**
 	* Register any application services.
 	*
 	* @return void
 	*/

	public function register()
	{
    //
	}

	/**
 	* Bootstrap any application services.
 	*
 	* @return void
 	*/

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

ref: https://laracasts.com/discuss/channels/general-discussion/syntax-error-or-access-violation-1071-specified-key-was-too-long

0 likes
3 replies
piljac1's avatar

Have you tried the alternative solution in the official doc?

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.

Tray2's avatar

@davy_yg I suggest updating your database to a later version or at least read the documentation

https://laravel.com/docs/7.x/migrations#creating-indexes

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 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 may configure this by calling the Schema::defaultStringLength method within your AppServiceProvider:

use Illuminate\Support\Facades\Schema;

/**
 * Bootstrap any application services.
 *
 * @return void
 */
public function boot()
{
    Schema::defaultStringLength(191);
}
1 like

Please or to participate in this conversation.