You can't run composer update and call artisan commands if add Schema::defaultStringLength calling to AppServiceProvider and you have doctrine/dbal package.
On running composer require doctrine/dbal:
In PDOConnection.php line 31:
SQLSTATE[HY000] [2002] Connection refused
In PDOConnection.php line 27:
SQLSTATE[HY000] [2002] Connection refused
On 5.8.12 works fine.
composer create-project --prefer-dist laravel/laravel blog
1071 Specified key was too long error:Add Schema::defaultStringLength calling to AppServiceProvider
use Illuminate\Support\Facades\Schema;
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
Schema::defaultStringLength(191);
}
doctrine/dbal package:composer require doctrine/dbal
You will get error:
In PDOConnection.php line 31:
SQLSTATE[HY000] [2002] Connection refused
In PDOConnection.php line 27:
SQLSTATE[HY000] [2002] Connection refused
Script @php artisan package:discover --ansi handling the post-autoload-dump event returned with error code 1
This is caused by #28214. /cc @JacksonIV
We should only call registerCustomDoctrineTypes() when we are actually running migrations.
@staudenmeir I鈥檒l look into this. 馃憤馃徏
I have tried a few things but I can't seem to reproduce this on a new laravel installation. Could you please provide me with some more information as where you encounter this error? Are you sure your database credentials and settings are correct?
Also, please make sure you are running:
composer require doctrine/dbal
instead of:
composer require docktrine/dbal
The error only happens when a database connection is not available.
Something like Schema::defaultStringLength(191); doesn't access the database and should work without a connection. Even if a connection is available, the builder shouldn't unnecessarily open one.
@staudenmeir Everything seems to work fine here, even without a connection. Maybe i'm doing something wrong though. Can you reproduce this on your Laravel installation?
Schema::defaultStringLength(); is a static method, so the constructor will not be called. Thefore the custom types won't be registered, right?
Yes, I can reproduce it:
doctrine/dbal installedSchema::defaultStringLength(191); in AppServiceProvider::boot()DB_CONNECTION=mysql in .envI can trigger the error with composer dump or php artisan serve.
The constructor does get called. Take a look at Illuminate\Support\Facades::getFacadeAccessor().
@staudenmeir I still have a hard time understanding how this can happen since the connection is first instantiated there?
@driesvints When the custom DBAL types get registered in the builder鈥檚 constructor it tries to find a Doctrine connection. If there鈥檚 no Doctrine connection, it will try to instantiate one. But if the database credentials or settings are incorrect it will throw a PDO exception.
If the database settings and credentials are correct, there鈥檚 no problem though. This could indeed be fixed by moving the actual registration to the migrator.
I will try to fix this as soon as i can, but unfortunately i鈥檓 a little short on time currently.
On the other hand, if you are calling the schema facade, you鈥檇 probably want a working database connection, right?
@driesvints The facade only instantiates an instance of the Connection class, but doesn't actually connect to the database. This happens in getPdo().
Is that what you mean?
@JacksonIV Probably, but not necessarily immediately. Only instantiating an instance of a class like MySqlBuilder (or any other class) should never open an actual database connection.
@staudenmeir I see what you mean. It's imperative that we get this fixed soon though. Otherwise it's best that we revert for now.
@driesvints Instead of reverting everything, i could make a PR which removes the registration of the custom DBAL types in the constructor of the MySQL builder. This way people can still call the registerCustomDBALType method manually without any problems.
I would revert it. Then we can take the time to fix this properly and also cover the other databases (https://github.com/laravel/framework/pull/28214#issuecomment-483012555).
@staudenmeir You're right, i'll start working on a proper fix as soon as possible. I'll also try to cover the other databases simultaneously!
Sorry for the hassle!
I've removed the method call in the constructor for now and this will be released tomorrow. Please send in a better solution as soon as you're able to, thanks!
@driesvints this also breaks the package:discover command that runs after composer install. I noticed this because our build process started failing as there is no .env and no database available yet at the composer install step in our build process.
After applying your revert from https://github.com/laravel/framework/pull/28301 it started working again.
Hopefully this can be tagged soon and properly addressed later. :)
I also receive this error if I call getDoctrineSchemaManager() within my console command constructor with laravel 6.2 and doctrine/dbal ^2.10
Doctrine\DBAL\Driver\PDOException : SQLSTATE[HY000] [2002] Connection refused
As soon as I move this call to the handle() method, it works as expected.
I receive this error when I call composer install!
in which version did you fix this issue??
Most helpful comment
@driesvints this also breaks the
package:discovercommand that runs aftercomposer install. I noticed this because our build process started failing as there is no.envand no database available yet at thecomposer installstep in our build process.After applying your revert from https://github.com/laravel/framework/pull/28301 it started working again.
Hopefully this can be tagged soon and properly addressed later. :)