【发布时间】:2023-04-03 06:19:01
【问题描述】:
我对 Doctrine Migrations 还很陌生,如果这真的很明显,我深表歉意。
更新以提供更多信息
我有一个 Symfony2 实体映射如下:
/**
* @ORM\Column(type="string")
*/
private $name;
这已添加到迁移中,并在一切正常运行的情况下进行部署。然后需要更新该列以接受空值,因此将其更改为:
/**
* @ORM\Column(type="string", nullable=true)
*/
private $name;
问题在于这对生成的迁移文件没有影响:
$ php app/console cache:clear
$ php app/console doctrine:migrations:diff
$ tail -50 app/DoctrineMigrations/Version20131028205742.php
<?php
namespace Application\Migrations;
use Doctrine\DBAL\Migrations\AbstractMigration;
use Doctrine\DBAL\Schema\Schema;
/**
* Auto-generated Migration: Please modify to your needs!
*/
class Version20131028205742 extends AbstractMigration
{
public function up(Schema $schema)
{
// this up() migration is auto-generated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() != "mysql", "Migration can only be executed safely on 'mysql'.");
}
public function down(Schema $schema)
{
// this down() migration is auto-generated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() != "mysql", "Migration can only be executed safely on 'mysql'.");
}
}
没有添加任何 ALTER 语句来处理空值。我可以删除并重建数据库,但由于已经部署了这个特定的迁移,这将导致生产数据库出现问题。我可以看到这种情况将来会再次发生,所以想多了解一下。
这是 Doctrine 和/或 Symfony2 Doctrine Migrations 捆绑包的限制吗?有没有办法在不编写自定义迁移的情况下解决这个问题?
请注意,我的所有其他迁移工作正常,唯一的问题是向现有字段添加可为空的选项。
【问题讨论】:
-
您在生成差异之前是否清除缓存?我认为这应该始终是发生学说映射问题时要问的第一个问题:D
-
是的,我清除了缓存。我不会想到这是我的问题的途径,尽管其他实体更新都被很好地拉了出来,而不是用于可空值的 ALTER 语句。
-
resulting diff file是什么意思? -
不会有
diff file- 无论是指什么。您的差异是您发布的内容,然后您运行doctrine:schema:update这将更新您的数据库。 -
正如我在回答中所说:如果您在更改实体后重建数据库,则差异将为空,因为数据库架构适合实体元数据。试试这个实验:1)将您的实体回滚为 nullable=false 2)重建您的数据库 3)将您的实体更改为 nullable=true 4)运行 diff(不重建数据库)。现在差异应该包含一个适当的改变。
标签: php mysql symfony doctrine-orm doctrine-migrations