Core: Unknown database error when database doesn’t exist (yet)

Created on 1 Aug 2016  Â·  5Comments  Â·  Source: api-platform/core

Hello,

In my project, I have a test database that’s frequently created and dropped. Problem is, once I install api-platform v2, an “unknown database 'my_db_test'” is raised whatever the action.

e.g.
$ php bin/console doctrine:database:create -e test

[Doctrine\DBAL\Driver\PDOException]
SQLSTATE[HY000] [1049] Unknown database 'my_db_test'

(I had the exact same issue with javiereguiluz/EasyAdminBundle a few months ago.)

Thank you for your help!

EDIT: interestingly, FOSUser has the same behavior as api-platform. I mean, if I remove one of them, it works. So it seems the combinaison of the two leads to this problem.

Important side note: It works perfectly with api-platform v1.

Most helpful comment

Alright. I think I fix this one.
So it was indirectly related to api-platform or FOSUserBundle. When working together, they somewhat loads FOSUser UserManager on any action. UserManager loads metadata which needs the database platform. Of course, I configure the driver for doctrine, but it seems it also need the server_version.

From the symfony/doctrine documentation:

The server_version option was added in Doctrine DBAL 2.5, which is used by DoctrineBundle 1.3. The value of this option should match your database server version (use postgres -V or psql -V command to find your PostgreSQL version and mysql -V to get your MySQL version).

If you don't define this option and you haven't created your database yet, you may get PDOException errors because Doctrine will try to guess the database server version automatically and none is available.

Problem solved ;)

All 5 comments

No stack trace ?

Here it is:

() at /var/www/project/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php:43
 PDO->__construct() at /var/www/project/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php:43
 Doctrine\DBAL\Driver\PDOConnection->__construct() at /var/www/project/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOMySql/Driver.php:41
 Doctrine\DBAL\Driver\PDOMySql\Driver->connect() at /var/www/project/vendor/doctrine/dbal/lib/Doctrine/DBAL/Connection.php:360
 Doctrine\DBAL\Connection->connect() at /var/www/project/vendor/doctrine/dbal/lib/Doctrine/DBAL/Connection.php:429
 Doctrine\DBAL\Connection->getDatabasePlatformVersion() at /var/www/project/vendor/doctrine/dbal/lib/Doctrine/DBAL/Connection.php:389
 Doctrine\DBAL\Connection->detectDatabasePlatform() at /var/www/project/vendor/doctrine/dbal/lib/Doctrine/DBAL/Connection.php:328
 Doctrine\DBAL\Connection->getDatabasePlatform() at /var/www/project/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php:763
 Doctrine\ORM\Mapping\ClassMetadataFactory->getTargetPlatform() at /var/www/project/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php:616
 Doctrine\ORM\Mapping\ClassMetadataFactory->completeIdGeneratorMapping() at /var/www/project/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php:174
 Doctrine\ORM\Mapping\ClassMetadataFactory->doLoadMetadata() at /var/www/project/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/AbstractClassMetadataFactory.php:332
 Doctrine\Common\Persistence\Mapping\AbstractClassMetadataFactory->loadMetadata() at /var/www/project/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php:78
 Doctrine\ORM\Mapping\ClassMetadataFactory->loadMetadata() at /var/www/project/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/AbstractClassMetadataFactory.php:216
 Doctrine\Common\Persistence\Mapping\AbstractClassMetadataFactory->getMetadataFor() at /var/www/project/vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php:281
 Doctrine\ORM\EntityManager->getClassMetadata() at /var/www/project/vendor/doctrine/orm/lib/Doctrine/ORM/Repository/DefaultRepositoryFactory.php:44
 Doctrine\ORM\Repository\DefaultRepositoryFactory->getRepository() at /var/www/project/vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php:698
 Doctrine\ORM\EntityManager->getRepository() at /var/www/project/vendor/friendsofsymfony/user-bundle/Doctrine/UserManager.php:40
 FOS\UserBundle\Doctrine\UserManager->__construct() at /var/www/project/var/cache/test/appTestDebugProjectContainer.php:2354
 appTestDebugProjectContainer->getFosUser_UserManagerService() at /var/www/project/vendor/symfony/symfony/src/Symfony/Component/DependencyInjection/Container.php:275
 Symfony\Component\DependencyInjection\Container->get() at /var/www/project/var/cache/test/appTestDebugProjectContainer.php:4605
 appTestDebugProjectContainer->getValidator_BuilderService() at /var/www/project/vendor/symfony/symfony/src/Symfony/Component/DependencyInjection/Container.php:275
 Symfony\Component\DependencyInjection\Container->get() at /var/www/project/var/cache/test/appTestDebugProjectContainer.php:4585
 appTestDebugProjectContainer->getValidatorService() at /var/www/project/vendor/symfony/symfony/src/Symfony/Component/DependencyInjection/Container.php:275
 Symfony\Component\DependencyInjection\Container->get() at /var/www/project/var/cache/test/appTestDebugProjectContainer.php:641
 appTestDebugProjectContainer->getApiPlatform_Metadata_Property_MetadataFactoryService() at /var/www/project/vendor/symfony/symfony/src/Symfony/Component/DependencyInjection/Container.php:275
 Symfony\Component\DependencyInjection\Container->get() at /var/www/project/var/cache/test/appTestDebugProjectContainer.php:4869
 appTestDebugProjectContainer->getApiPlatform_Swagger_Normalizer_DocumentationService() at /var/www/project/vendor/symfony/symfony/src/Symfony/Component/DependencyInjection/Container.php:275
 Symfony\Component\DependencyInjection\Container->get() at /var/www/project/var/cache/test/appTestDebugProjectContainer.php:719
 appTestDebugProjectContainer->getApiPlatform_Swagger_Command_SwaggerCommandService() at /var/www/project/vendor/symfony/symfony/src/Symfony/Component/DependencyInjection/Container.php:275
 Symfony\Component\DependencyInjection\Container->get() at /var/www/project/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Console/Application.php:123
 Symfony\Bundle\FrameworkBundle\Console\Application->registerCommands() at /var/www/project/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Console/Application.php:98
 Symfony\Bundle\FrameworkBundle\Console\Application->all() at /var/www/project/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Console/Application.php:72
 Symfony\Bundle\FrameworkBundle\Console\Application->doRun() at /var/www/project/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php:117
 Symfony\Component\Console\Application->run() at /var/www/project/bin/console:29

I don't see any ApiPlatform related line, are you sure this is related to this package?

From what I read, FOS\UserBundle\Doctrine\UserManager->__construct() is called trying to get the database to fetch the user, resulting in your error.

I see these: appTestDebugProjectContainer->getApiPlatform_Metadata_Property_MetadataFactoryService()
appTestDebugProjectContainer->getApiPlatform_Swagger_Normalizer_DocumentationService()
appTestDebugProjectContainer->getApiPlatform_Metadata_Property_MetadataFactoryService()

It may be indirectly related to api platform though, but the thing is, when I comment api-platform bundle in AppKernel.php, everything works.

EDIT: interestingly, FOSUser has the same behavior as api-platform. I mean, if I remove one of them, it works. So it seems the combinaison of the two leads to this problem.

Important side note: It works perfectly with api-platform v1.

Alright. I think I fix this one.
So it was indirectly related to api-platform or FOSUserBundle. When working together, they somewhat loads FOSUser UserManager on any action. UserManager loads metadata which needs the database platform. Of course, I configure the driver for doctrine, but it seems it also need the server_version.

From the symfony/doctrine documentation:

The server_version option was added in Doctrine DBAL 2.5, which is used by DoctrineBundle 1.3. The value of this option should match your database server version (use postgres -V or psql -V command to find your PostgreSQL version and mysql -V to get your MySQL version).

If you don't define this option and you haven't created your database yet, you may get PDOException errors because Doctrine will try to guess the database server version automatically and none is available.

Problem solved ;)

Was this page helpful?
0 / 5 - 0 ratings