Laravel 5: Specified key was too long error

By Author | May 08, 2019 | | 113 Views | Category : Laravel

As many of you may have faced this error particularly when adding new tables to database or while running migration command. This error is generated when default string length set in your Database configurations is less than what is required by laravel migrations. This has a simple fix also given in official documentation which we will see just in a moment.

But first, we need to identify why this happens. Laravel uses the utf8mb4 character set by default, which includes support for storing "emojis" in the database. So if you are running Mysql database with version older than 5.7 or if you are running MariaDB which has a version older than 10.2 then you will face this problem because in these versions the string length is shorter than what is expected by migrations.

You may configure this value manually in your project in AppServiceProviders.php file as given below


     * Bootstrap any application services.
     * @return void
    public function boot()

Or you may also enable "innodb_large_prefix" in your DB directly so that it will effect all your laravel projects.
You may include below line in your "my.cnf" file which is your Mysql configurations file and then restart your mysql server.


innodb_file_format = Barracuda
innodb_large_prefix = 1
innodb_file_per_table = ON

if the above file is not present then you need to create a new file with same name. And its locations may vary in xampp/mamp/wamp but i can tell you in mamp its location is "MAMP/conf/my.cnf". Similarly you can find for others.

Hope this article have solved your problem.

Tags :