【问题标题】:How to prevent doctrine from creating useless migrations with DEFAULT NULL如何防止教义使用 DEFAULT NULL 创建无用的迁移
【发布时间】:2020-06-16 06:08:19
【问题描述】:

我想向那些使用 Mysql + Doctrine ORM + Doctrine 迁移的人提出问题 我有一个协会:

    /**
     * @var User
     *
     * @ORM\ManyToOne(targetEntity="User")
     * @ORM\JoinColumn(nullable=true)
     */
    protected $sender;

(这里的重点是nullable=true。有些属性是故意留下的)

  1. 我做migraions:diff - 用一块sender_id VARCHAR(255) DEFAULT NULL生成迁移
  2. 运行迁移
  3. 然后我再次diff预计没有新的迁移生成
  4. 但是没有,我看到新文件的行是CHANGE sender_id sender_id VARCHAR(255) DEFAULT NULL

这意味着什么都不会改变,但是Mysql驱动下的doctrine dbal没有从实体注释中看到DEFAULT NULL

我尝试添加@ORM\Column(options={"default": NULL})

这有助于防止 DEFAULT NULL 在迁移中发生,但 FK 和 Index 在这种情况下被删除了。

也尝试过添加columnDefinition="VARCHAR(255) DEFAULT NULL",但也没有用

你是怎么解决这个问题的?

【问题讨论】:

    标签: php doctrine-orm doctrine-migrations


    【解决方案1】:

    我有同样的问题。就我而言(Symfony,使用带有图像“mariadb:10.5.8”的 Docker),解决方案是在配置中设置正确的server_version

    doctrine:
        dbal:
            server_version: '5.7' # (this was previously incorrect in my setting!)
    

            server_version: 'mariadb-10.5.8' # (correct version for my setting, please adjust accordingly!)
    

    这样做并重新启动 Docker 后,diff 为空(“在您的映射信息中未检测到更改。”)。

    所有功劳归于“tristanbes 于 2018 年 4 月 4 日发表评论”的帖子,请参阅以下链接:

    https://github.com/doctrine/dbal/issues/2985

    【讨论】:

    • 请注意,'server_version' 参数可以被 DATABASE_URL 变量(.env 或 .env.load 配置文件)覆盖。如果存在,您必须更改“serverVersion”参数。 DATABASE_URL=mysql://user:password@127.0.0.1:3306/database?serverVersion=mariadb-10.5.11
    • 我的评论中有错字:.env 或 .env.local
    • 遗憾的是,这在使用 DoctrineMigrationBundle 时没有任何区别,它会不断重新生成无用的 sql 语句,例如枚举更改、删除外键只是为了重新创建它们等等......
    猜你喜欢
    • 2018-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-25
    • 1970-01-01
    • 2021-08-30
    • 2018-10-14
    • 1970-01-01
    相关资源
    最近更新 更多