Doctrine - orm:schema-tool:create - 如何忽略已经存在的表?

时间:2016-11-29 00:05:53

标签: php mysql orm doctrine-orm

我需要在我的数据库中创建一些新表,所以我运行orm:schema-tool:create并创建了第一个表,但后来尝试创建一个已经存在且脚本停止运行的表。下面是它输出的错误信息:

  [Doctrine\ORM\Tools\ToolsException]
  Schema-Tool failed with Error 'An exception occurred while executing 'CREATE TABLE autogen_structures (id INT AUTO_
  INCREMENT NOT NULL, structure_id INT DEFAULT NULL, INDEX FK_STRUCTURES_TO_AUTOGEN_STRUC (structure_id), PRIMARY KEY
  (id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB':
  SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'autogen_structures' already exists' while executing
   DDL: CREATE TABLE autogen_structures (id INT AUTO_INCREMENT NOT NULL, structure_id INT DEFAULT NULL, INDEX FK_STRU
  CTURES_TO_AUTOGEN_STRUC (structure_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE
  = InnoDB



  [Doctrine\DBAL\Exception\TableExistsException]
  An exception occurred while executing 'CREATE TABLE autogen_structures (id INT AUTO_INCREMENT NOT NULL, structure_i
  d INT DEFAULT NULL, INDEX FK_STRUCTURES_TO_AUTOGEN_STRUC (structure_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf
  8 COLLATE utf8_unicode_ci ENGINE = InnoDB':
  SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'autogen_structures' already exists



  [Doctrine\DBAL\Driver\PDOException]
  SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'autogen_structures' already exists

我真的很放心,它没有消除所有现有表的数据,我之前遇到过这个问题,所以我假设它是orm:schema-tool:update导致了这个问题。在表中包含数千行的数据库中,这是一个非常大的问题,因为从备份文件重新创建数据库需要很长时间。

是否有办法创建新表,并忽略现有表。还有一种方法可以更新现有表的模式而不会清空其中的数据吗?

1 个答案:

答案 0 :(得分:0)

修改 不幸的是,这个问题并没有完全解决。我尝试再次运行,这次它没有用,我收到了这个错误:

  [Doctrine\DBAL\Exception\DriverException]
  An exception occurred while executing 'ALTER TABLE compounds CHANGE id id INT AUTO_INCREMENT NOT NULL':
  SQLSTATE[HY000]: General error: 1833 Cannot change column 'id': used in a foreign key constraint 'FK_5BBEC55AAE76FC
  6F' of table 'chebi_backup.structures'

第一次运行命令时出现了这些外键约束,所以当我第二次运行命令时,我不明白为什么它会成为问题。如果它不允许我更新架构,我无法使用doctrine。有没有解决的办法?

编辑之前: 问题解决了(至少对于MySQL)。我在备份表上运行orm:schema-tool:update --dump-sql --force以查看它是否会清除现有表中的数据,但事实并非如此,表仍然保持不变。查看查询,我看到它没有尝试创建现有表,它只创建了新表。

数据被删除的问题出在sqlite数据库上。这次我正在使用MySQL数据库。我仍然不知道为什么之前数据被消灭了,它有点令人担忧,因为如果我不知道发生了什么,它可能会再次发生。