3

I started a PHP project with the lumen framework.

When I try to execute the command php artisan migrate:fresh from the console I get the following error

SQLSTATE[42000]: Syntax error or access violation: 1071

It is a well known error in Laravel but this question is not a duplicate because there is no boot method on lumen, as the documentation says :

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.

However, when I try to use this fix in the microframework Lumen the error is still there.

Léo Coletta
  • 1,099
  • 2
  • 12
  • 24
  • What version of **MySQL** / **MariaDB** are you using? Can you show us your attempt and where you did it? – Clément Baconnier Mar 04 '19 at 21:46
  • Possible duplicate of [Laravel migration: unique key is too long, even if specified](https://stackoverflow.com/questions/23786359/laravel-migration-unique-key-is-too-long-even-if-specified) – Clément Baconnier Mar 04 '19 at 21:48

2 Answers2

1

This error basically comes from you have not set defaultStringLength in boot() function

[PDOException] SQLSTATE[42000]: Syntax error or access violation: 1071

SOLUTION:

Just Add the below code in app/Providers/AppServiceProvider.php method:

use Illuminate\Support\Facades\Schema;

public function boot()
{
    Schema::defaultStringLength(191);
}
Udhav Sarvaiya
  • 9,380
  • 13
  • 53
  • 64
  • 1
    Only my opinion: I don't like this solution since it reduce the `string` column from 255 to 191 characters. When I used to had the same error (I guess we all did :)) I chose [this answer](https://stackoverflow.com/a/39750202/8068675) I didn't needed to change `max:255` to `max:191` on all the validations and back when I upgraded MariaDB. And this answer actually solve the problem, instead of providing a fix. – Clément Baconnier Mar 05 '19 at 07:55
  • The boot method must be inside the class ? @UdhavSarvaiya – Léo Coletta Mar 05 '19 at 15:46
  • https://github.com/laravel/laravel/blob/master/app/Providers/AppServiceProvider.php check here – Udhav Sarvaiya Mar 05 '19 at 16:09
  • @UdhavSarvaiya Yes it's what I did, but no it doesn't solve the issue – Léo Coletta Mar 05 '19 at 16:26
  • @cbaconnier The problem is that there is no config/database.php in lumen – Léo Coletta Mar 05 '19 at 16:31
1

In Lumen you have also to uncomment the line:

$app->register(App\Providers\AppServiceProvider::class); In bootstrap/app.php

Udhav Sarvaiya
  • 9,380
  • 13
  • 53
  • 64
Léo Coletta
  • 1,099
  • 2
  • 12
  • 24