6

我有一个现有的 PostgreSQL 数据库,其中创建了一个像这样的表:

CREATE TABLE product (id SERIAL PRIMARY KEY, name VARCHAR(100) DEFAULT NULL)

该表在 Symfony2 项目中的 YML Doctrine2 文件中进行了描述:

Acme\DemoBundle\Entity\Product:
    type: entity
    table: product
    fields:
        id:
            id: true
            type: integer
            nullable: false
            generator:
                strategy: SEQUENCE
        name:
            type: string
            length: 100
            nullable: true

当我第一次运行 Doctrine Migrations diff 任务时,我应该得到一个在upanddown方法中没有数据的版本控制文件。但我得到的是:

// ...

class Version20120807125808 extends AbstractMigration
{
    public function up(Schema $schema)
    {
        // this up() migration is autogenerated, please modify it to your needs
        $this->abortIf($this->connection->getDatabasePlatform()->getName() != "postgresql");

        $this->addSql("ALTER TABLE product ALTER id DROP DEFAULT");
    }

    public function down(Schema $schema)
    {
        // this down() migration is autogenerated, please modify it to your needs
        $this->abortIf($this->connection->getDatabasePlatform()->getName() != "postgresql");

        $this->addSql("CREATE SEQUENCE product_id_seq");
        $this->addSql("SELECT setval('product_id_seq', (SELECT MAX(id) FROM product))");
        $this->addSql("ALTER TABLE product ALTER id SET DEFAULT nextval('product_id_seq')");
    }
}

为什么会检测到差异?我怎样才能避免这种情况?我尝试了几种序列策略,但均未成功。

4

3 回答 3

5

关于这个问题的一点更新。

使用 Doctrine 2.4,解决方案是使用IDENTITY生成器策略:

Acme\DemoBundle\Entity\Product:
    type: entity
    table: product
    id:
        type: integer
        generator:
            strategy: IDENTITY
    fields:
        name:
            type: string
            length: 100
            nullable: true

为了避免DROP DEFAULT在数据库中具有默认值的字段,default字段上的选项是要走的路。当然,这可以通过生命周期回调来完成,但如果该数据库被其他应用程序使用,则必须在数据库中保留默认值。

对于像默认值这样的“DEFAULT NOW()”,解决方案如下:

Acme\DemoBundle\Entity\Product:
    type: entity
    table: product
    id:
        type: integer
        generator:
            strategy: IDENTITY
    fields:
        creation_date:
            type: datetime
            nullable: false
            options:
                default: CURRENT_TIMESTAMP
于 2014-04-09T13:10:54.020 回答
2

Doctrine 2.0 不支持 SQL DEFAULT 关键字,并且总是会尝试删除 postgres 默认值。

我没有找到解决这个问题的方法,我只是让学说自己处理序列。

于 2012-11-23T05:53:16.197 回答
1

这是在此处注册的已打开错误:

http://www.doctrine-project.org/jira/browse/DBAL-903

于 2015-07-14T16:14:42.757 回答