You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have local project that I'm setting up that uses docker for DB.
I've updated .env for DATABASE_URL env var to reflect what db url is but after serving that project with symfony serve errors appear Unknown database 'root'
After digging a bit in what's happening, it appears that symfony CLI is not using .env file from the project as it should, but it rather appears that it's using some other .env file when my docker is up.
Steps to reproduce:
install default symfony app
use docker for DB with provided yaml file docker-compose.yaml
update your .env file to use DB that you want (in my case it's DATABASE_URL="mysql://root:password@127.0.0.1:3306/symfony?serverVersion=mariadb-11.2.2"
run command to create DB php bin/console doctrine:schema:create and your db symfony should be created
(optional) if you run symfony console doctrine:schema:create you'll see message that db root was created
Findings:
Now where does this error comes from as my db name is symfony and not root.
After checking symfony console debug:container --env-vars I've got this result: DATABASE_URL "mysql://root:password@127.0.0.1:3306/root?sslmode=disable&charset=utf8mb4&serverVersion=1:11.2.2+maria~ubu2204" which means Symfony CLI didn't actually use my local .env file but rather something else.
Then it came to my mind to check what will happen if I stop docker DB, and re-run previous command. Response was different and I got what was actually in my defined DATABASE_URL: DATABASE_URL "mysql://root:password@127.0.0.1:3306/symfony?serverVersion=mariadb-11.2.2"
What should be expected behaviour?
Symfony CLI should use projects defined .env file
So after reading CLI docs and codebase it looks like this is expected behaviour.
I would strongly suggest that this should be disabled by default, and enabled with some flag or config param e.g. in .symfony.local.yaml
Using Symfony CLI in Symfony project with Docker DB is somewhat now default scenario for some local development etc., unless you go full docker setup for your Symfony project. By default it's expected from developer to configure it's .env file for DB params etc, and with this "magic" it makes a lot of trouble and drawback, especially debugging what is going on, and why is it doing this when I didn't define it at all.
This took me couple hours from my coding session to debug (RTFM lesson for me I guess in this case), but regardless, this was not expected behaviour of the CLI.
For those who are looking for solution and disabling this, as documentation says, you should add these two lines in your docker-compose.yaml
I have local project that I'm setting up that uses docker for DB.
I've updated
.env
forDATABASE_URL
env var to reflect what db url is but after serving that project withsymfony serve
errors appearUnknown database 'root'
After digging a bit in what's happening, it appears that symfony CLI is not using
.env
file from the project as it should, but it rather appears that it's using some other.env
file when my docker is up.Steps to reproduce:
docker-compose.yaml
docker-compose up -d
.env
file to use DB that you want (in my case it'sDATABASE_URL="mysql://root:password@127.0.0.1:3306/symfony?serverVersion=mariadb-11.2.2"
php bin/console doctrine:schema:create
and your dbsymfony
should be createdsymfony console doctrine:schema:create
you'll see message that dbroot
was createdFindings:
Now where does this error comes from as my db name is
symfony
and notroot
.After checking
symfony console debug:container --env-vars
I've got this result:DATABASE_URL "mysql://root:password@127.0.0.1:3306/root?sslmode=disable&charset=utf8mb4&serverVersion=1:11.2.2+maria~ubu2204"
which means Symfony CLI didn't actually use my local.env
file but rather something else.Then it came to my mind to check what will happen if I stop docker DB, and re-run previous command. Response was different and I got what was actually in my defined
DATABASE_URL
:DATABASE_URL "mysql://root:password@127.0.0.1:3306/symfony?serverVersion=mariadb-11.2.2"
What should be expected behaviour?
Symfony CLI should use projects defined
.env
fileSymfony CLI Version:
Symfony CLI version 5.8.7 (c) 2021-2024 Fabien Potencier (2024-02-08T11:42:34Z - stable)
This issue could be related to #425 and #152
The text was updated successfully, but these errors were encountered: