Skip to content

Instantly share code, notes, and snippets.

@yavgel85
Last active October 29, 2021 08:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yavgel85/8d7affd6cc2cc942f7d7be19a2b08e2e to your computer and use it in GitHub Desktop.
Save yavgel85/8d7affd6cc2cc942f7d7be19a2b08e2e to your computer and use it in GitHub Desktop.
Specified key was too long #laravel #sql
<?php
// SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes
use Illuminate\Support\Facades\Schema;
public function boot()
{
Schema::defaultStringLength(191);
}
# OR THIS WAY
# Laravel's Blueprint $table accepts a raw statement, so you can define it this way
Schema::table('table_name', function (Blueprint $table) {
$table->index([DB::raw('field_name(50)')], 'index_name');
});
# index_name is optional as Laravel neatly prefixes table name to field name, so it becomes table_name_field_name
# As to why this error occurs and finding the ideal index size, this is a great answer on Stackoverflow
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment