Mic*_*rin 5 postgresql symfony doctrine-orm
我有一个现有的PostgreSQL数据库,其中包含如下所示的表:
CREATE TABLE product (id SERIAL PRIMARY KEY, name VARCHAR(100) DEFAULT NULL)
Run Code Online (Sandbox Code Playgroud)
该表在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
Run Code Online (Sandbox Code Playgroud)
当我第一次运行Doctrine Migrations diff任务时,我应该得到一个版本控制文件,其中没有数据up和down方法.但我得到的是这样的:
// ...
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')");
}
}
Run Code Online (Sandbox Code Playgroud)
为什么会发现差异?我怎么能避免这个?我尝试了几种顺序策略但没有成功.
关于这个问题的一点更新。
使用 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
Run Code Online (Sandbox Code Playgroud)
为了避免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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1133 次 |
| 最近记录: |