Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

danielonyinye's avatar

Change $table->string('email' 250)->unique(); to $table->string('email')->unique(); by removing the 250 attached to the email and

If you are using MariaDB or an older version of MySQL, you need to place this code in your AppServiceProvider.php:

use Illuminate\Support\Facades\Schema;

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

and run php artisan migrate.

Snapey's avatar

@danielony why would you want to make every string 191 long when it only affects strings that need to be unique or indexed

krishna9720's avatar

Edit your Boot Method Inside AppServiceProvider and add this line:-

Schema::defaultStringLength(191);

Thanks.

Snapey's avatar

just don't be lazy and set the string length according to your needs.

It's a recipie for later confusion to tuck a setting away somewhere that changes the default string length. @krishna9720 gets a big thumbs-down from me..

2 likes
nasersina's avatar

SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 1000 bytes (SQL: alter table available_dat as add unique type_length_width_thickness_grade_fee(type, length, width, thickness, grade, fee))

Grelav's avatar

The solution is to create database with collation type to: utf8mb4_unicode_ci

autefrum's avatar

I agree with the solutions above - but for a single line solution, combining the use and the extra line in boot, you can use:

\Illuminate\Support\Facades\Schema::defaultStringLength(191);

inside the boot function in app\Providers\AppServiceProvider.php

helal.uddin's avatar

For MYSQL, This works fine.

Add following commands on the schemas.

$table->engine = 'InnoDB'; $table->charset = 'utf8'; $table->collation = 'utf8_unicode_ci';

Thanks!!

1 like
Cronix's avatar

@helal.uddin You can also just set those options in the database config and then not have to do it for each migration.

sheikma's avatar

@SNAPEY - Thanks for the fix @snapey Your comments and fixes are always easy to understand and works great.

msyadav's avatar

I placed the same below code use Illuminate\Support\Facades\Schema; public function boot() { Schema::defaultStringLength(191); }

, but while installing spatie/ roles and permissions package and doing migrate , it gives error : Syntax error or access violation: 1071 Specified key was too long; max key length is 1000 bytes (SQL: alter table permissions add unique permissions_name_guard_name_unique(name, guard_name))

User1980's avatar

To anyone with this issue. It is because you are migrating tables as MyISAM instead of InnoDB. If you check your local database vs the remote missed migrations you did, I bet you the local is creating InnoDB tables and the remote one MyISAM.

All you have to do is go to the config/database.php file and add this on your remote and local copy:

     'mysql' => [
            'driver' => 'mysql',
            'url' => env('DATABASE_URL'),
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'prefix_indexes' => true,
            'strict' => true,
            'engine' => 'InnoDB',  <----------this one
            'options' => extension_loaded('pdo_mysql') ? array_filter([
                PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
            ]) : [],
        ],

Make sure you run these commands remotely after the change:

php artisan cache:clear
php artisan config:clear
php artisan config:cache
2 likes
RetroCIB's avatar

changing in phpmyadmin from latin1_swedish_ci collation to utf8mb4_unicode_ci + 'engine' => 'InnoDB' in config/database.php (see @user1980 ) worked for me

yogeshMore's avatar

Go inside config/database.php and find mysql=>engine and replace null with 'InnoDB' simply.

Abby2727's avatar

2024:

Adding Schema::defaultStringLength(191); in your AppServiceProvider.php might work but in case if not you need to modify your database.php to:

'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'engine' => env('DB_ENGINE', 'InnoDB'),

make sure to optimize first (or closing and opening your teminal/ide) before migrating again. php artisan optimize

me108's avatar

Seeing this thread is over 7 years old, I wonder why I get this error on a fresh Laravel 11.9 installation with MariaDB 11.5.2?

PHP version 8.2.25
MariaDB version 11.5.2
Laravel version 11.9

0001_01_01_000001_create_password_reset_tokens_table.php

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('password_reset_tokens', function (Blueprint $table) {
            $table->string('email')->primary();
            $table->string('token');
            $table->timestamp('created_at')->nullable();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('password_reset_tokens');
    }
};
> php artisan migrate

INFO  Running migrations.

  0001_01_01_000001_create_password_reset_tokens_table ................................................... 11.23ms FAIL

   Illuminate\Database\QueryException

  SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 1000 bytes (Connection: mariadb, SQL: create table `password_reset_tokens` (`email` varchar(255) not null, `token` varchar(255) not null, `created_at` timestamp null, primary key (`email`)) default character set utf8mb4 collate 'utf8mb4_unicode_ci')

at ...

Edit

Adding Schema::defaultStringLength(191); to the migration solves the problem. But I'm curious why this is still a thing seven years later?

Previous

Please or to participate in this conversation.