[英]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.我有一个 Symfony 5 应用程序,其中包括三个 doctrine 迁移。 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.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. ... per this answer on a different question ,但这并不能解决问题。 What am I doing wrong here?
我在这里做错了什么?
==== ====
EDIT 1: Here is the content of doctrine_migrations.yaml:编辑 1:这是教义迁移的内容。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.编辑 2:我仔细检查以确保我的三个迁移中没有一个包含
migration_versions
表的创建。 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:编辑 3:我运行了这三个命令:
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. ...并且每次都返回
Symfony 5.0.1 (env: dev, debug: true)
,没有出现错误。 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.每次运行迁移命令时,doctrine 迁移器都会检查迁移表是否存在。 The migrator checks by doctrine schema manager .
迁移器通过doctrine 模式管理器进行检查。 It seems that in your situation the isInitialized method always returns
false
.似乎在您的情况下isInitialized方法总是返回
false
。
You can debug the isInitialized
method.您可以调试
isInitialized
方法。 Especially rows $this->schemaManager->tablesExist([$this->configuration->getTableName()])
特别是行
$this->schemaManager->tablesExist([$this->configuration->getTableName()])
Doctrine checks if a table exists by getting a list of tables from information_schema (for MySQL). Doctrine 通过从 information_schema(对于 MySQL)获取表列表来检查表是否存在。 The SQL will be something like that:
SQL 将是这样的:
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.在您的情况下,您的数据库中的
information_schema.tables
和表之间似乎存在不同步。
The faster fix: change your database name更快的解决方法:更改您的数据库名称
ry to run the following commands: ry 运行以下命令:
bin/console doctrine:schema:update bin/控制台 doctrine:schema:update
doctrine:schema:update --force to execute the command doctrine:schema:update --dump-sql to dump the SQL statements to the screen doctrine:schema:update --force 执行命令 doctrine:schema:update --dump-sql 将 SQL 语句转储到屏幕
to see:: https://symfony.com/doc/current/DoctrineMigrationsBundle/index.html看:: https://symfony.com/doc/current/DoctrineMigrationsBundle/index.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.