Symfony2 + Doctrine2 SQL 完整性冲突:可为空的字段不能为空


Symfony2 + Doctrine2 SQL Integrity Violation: Nullable field can't be null?

所以这个项目正在将一些直接的PDOSQL查询(使用MySQL)转移到Doctrine。这些表已存在,并且其中包含数据。我设置了实体,一切似乎都是金色的。所有查询都用 Doctrine 中的实体管理器重写,一切似乎都正常。

除了我忘记了其中一个实体中的三个字段需要为空。它们在数据库中被设置为 NOT NULL,并且在教义注释中没有 nullable=true 声明。

我在 MySQL 中手动更改为表以使字段可为空,然后将 nullable=true 添加到实体,清除缓存并执行

php app/console doctrine:schema:update --force

只是为了确保(正确执行)。

但是,尽管 Doctrine 实体允许空字段,并且数据库的列也可为空,但我仍然收到此错误:

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'type_id' cannot be null

有没有办法在不删除表的情况下解决此问题(这也涉及保存所有数据)?这里到底出了什么问题?根据涉及的所有代码和数据库,根本不应该有完整性违规(该字段也不是外键)。

这是有问题的实体:

class Audio
{
    /**
     * @ORM'Id
     * @ORM'Column(type="integer", name="id")
     * @ORM'GeneratedValue(strategy="AUTO")
     */
    protected $id;
    /**
     * @ORM'Column(type="string", name="type", columnDefinition="ENUM('song','demo')")
     */
    protected $type;
    /**
     * @ORM'Column(type="integer", name="type_id", nullable=true)
     */
    protected $typeId = null;
    /**
     * @ORM'Column(type="string", name="s3_url", nullable=true)
     */
    protected $s3url = null;
    /**
     * @ORM'Column(type="string", name="s3_key", nullable=true)
     */
    protected $s3key = null;
    /**
     * @ORM'Column(type="datetime", name="date_created")
     */
    protected $dateCreated;
    /**
     * @ORM'Column(type="datetime", name="date_modified")
     */
    protected $dateModified;

这绝对让我感到困惑。我真的没有更多的方法告诉程序它可以为空。

嗨,

我不知道你是否还有问题。

你可以做php app/console doctrine:schema:update --dump-sql为了了解模型和数据库之间的差异。并应用他会告诉你的台词。

我认为它会解决问题