没有外键约束的 Doctrine 2 关联

2022-01-03 00:00:00 database php symfony doctrine-orm

我正在将遗留 PHP 应用程序转换为 Symfony 2.目前应用程序数据不是很一致,所以我想避免创建外键约束.我的产品"实体类中有以下注释:

I'm in the process of converting a legacy PHP application to Symfony 2. The application data is not very consistent at the moment, so I would like to avoid creating foreign key constraints. I have the following annotation in my "Product" entity class:

class Product {

  // some definitions

  /**
   * @ORMManyToOne(targetEntity="Manufacturer")
   * @ORMJoinColumn(name="manufacturer_id", referencedColumnName="id" )
   */
  private $Manufacturer;
}

当我执行app/console police:schema:update时,我得到了SQL命令

When I do app/console doctrine:schema:update, I get the SQL command

 ALTER TABLE products ADD CONSTRAINT FK_F6FA18741C3BF575 
   FOREIGN KEY (manufacturer_id) REFERENCES manufacturer(id);

我怎样才能避免这种情况?

How can I avoid this?

推荐答案

我最近也经历了同样的过程,幸好有一个简单的解决方案,只需将 nullable=true 添加到列的注释中.

I had to go through the same process recently and fortunately there is an easy solution, just add nullable=true to the column's annotation.

只要现有数据有效,这就会起作用,在我的情况下,我必须将 0 更改为 NULL,并将不再存在的键更改为 NULL.

This will work as long as the existing data is valid, in my case I had to change 0's to NULL's and change keys that didn't exist anymore to NULL.

相关文章