Search code examples
phplaravellaravel-passportlaravel-migrations

Laravel testing database migration fails due to Passport


I installed Laravel Passport and migrated the new tables just fine in my database. But when I try to run migrations for the testing database, ie. artisan migrate --database=testing I get:

SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'oauth_auth_codes' already exists

The only table that exists in the testing database is the migrations table. It looks as if it's checking the regular database and seeing this Passport table instead of checking the testing database. Did I miss something simple here?


Solution

  • php artisan ... commands will use your .env variables for configuration, so yes, unless you specified which database connection to use, it will use your default .env's DB_ variables.

    You can define something like a .env.testing, then re-run as php artisan migrate --env=testing to use that file instead of your default one.

    .env:

    DB_CONNECTION=mysql
    DB_DATABASE=database
    ...
    # Host, Port, Username, Password, 
    

    .env.testing:

    DB_CONNECTION=mysql
    DB_DATABASE=testing
    ...
    # Host, Port, Username, Password,