Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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;
}

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
687 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...