简体   繁体   中英

Symfony 5: Base table or view already exists: 1050 Table 'migration_versions' already exists

I have a Symfony 5 app, which includes three doctrine migrations. I try to run the following commands:

bin/console doctrine:database:drop --force
bin/console doctrine:database:create
bin/console doctrine:migrations:migrate

... but when running the third command, I get these errors:

In AbstractMySQLDriver.php line 38:

An exception occurred while executing 'CREATE TABLE migration_versions (version VARCHAR(14) NOT NULL, executed_a
  t DATETIME NOT NULL COMMENT '(DC2Type:datetime_immutable)', PRIMARY KEY(version)) DEFAULT CHARACTER SET utf8mb4
  COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB':

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


In PDOConnection.php line 43:

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


In PDOConnection.php line 41:

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

I have tried adding the following code in doctrine.yaml :

doctrine:
    dbal:
        url: '%env(resolve:DATABASE_URL)%'
        schema_filter: ~^(migration_versions)$~

... per this answer on a different question , but this does not solve the problem. What am I doing wrong here?

====

EDIT 1: Here is the content of doctrine_migrations.yaml:

doctrine_migrations:
    dir_name: '%kernel.project_dir%/src/Migrations'
    # namespace is arbitrary but should be different from App\Migrations
    # as migrations classes should NOT be autoloaded
    namespace: DoctrineMigrations

EDIT 2: I double-checked to make sure that none of my three migrations contains a creation of the migration_versions table. It's not being created in any of those three migrations. So it's very mysterious where the error is coming from.

EDIT 3: I ran these three commands:

bin/console doctrine:migrations:execute --up --version 20191210174025
bin/console doctrine:migrations:execute --up --version 20201219113811
bin/console doctrine:migrations:execute --up --version 20201221174858

... and got back Symfony 5.0.1 (env: dev, debug: true) each time, with no error appearing. So I don't think the problem is in the migrations themselves.

Every time when you run the migration command, doctrine migrator check if the migration table exists. The migrator checks by doctrine schema manager . It seems that in your situation the isInitialized method always returns false .

You can debug the isInitialized method. Especially rows $this->schemaManager->tablesExist([$this->configuration->getTableName()])

Doctrine checks if a table exists by getting a list of tables from information_schema (for MySQL). The SQL will be something like that:

SELECT table_name
FROM information_schema.tables
WHERE table_schema = 'databasename'

It seems in your case there is desynchronization between information_schema.tables and tables in your database.

The faster fix: change your database name

ry to run the following commands:

bin/console doctrine:schema:update

doctrine:schema:update --force to execute the command doctrine:schema:update --dump-sql to dump the SQL statements to the screen

to see:: https://symfony.com/doc/current/DoctrineMigrationsBundle/index.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM