Home > Article > Backend Development > Why Am I Getting a \"Unique Key Is Too Long\" Error During Laravel Migrations?
Troubleshooting Laravel Migration Errors: "Unique Key Is Too Long"
While attempting to migrate a users table in Laravel, you may encounter the error: "[IlluminateDatabaseQueryException] ... Specified key was too long; max key length is 767 bytes." This error arises when the unique key you've specified exceeds the maximum length allowed.
Understanding the Issue
The default string length in Laravel for columns like email is 255 characters. When you attempt to create a unique key on a column with a longer string length (in this case, 320 characters for the email column), the migration fails.
Fixing the Error
<code class="php">$table->string('email', 250);</code>
<code class="php">$table->string('email');</code>
In the AppServiceProvider.php file, in the boot method, set the default string length for all migrations:
<code class="php">use Illuminate\Database\Schema\Builder; public function boot() { Builder::defaultStringLength(191); }</code>
The above is the detailed content of Why Am I Getting a \"Unique Key Is Too Long\" Error During Laravel Migrations?. For more information, please follow other related articles on the PHP Chinese website!