Reputation: 974
I have an existing entity in database. I would like to add a new column to this entity:
/**
* @ORM\ManyToOne(targetEntity="Language")
* @ORM\JoinColumn(name="language_id", referencedColumnName="id", nullable=false)
*/
protected $language;
When I now use "vendor/bin/doctrine-migrate migrations:diff"
, the generated migration script does not contain a default value for language_id
. Therefore the migration-script fails. Setting a default value for the property in the object does not help. How can I define a default value for the fk-column? I neither found something in doctrines documentation nor through google/stackoverflow.
Upvotes: 3
Views: 3623
Reputation: 456
It may not be a clean way to do it, but maybe you can execute the SQL query yourself and add manually the DEFAULT statement ?
ALTER TABLE registered_user ADD language_id VARCHAR(255) NOT NULL DEFAULT {default_value} WITH VALUES;
I'm quite surprised that adding a default property in the annotation is not working here!
Upvotes: 1
Reputation: 9857
If the column is not null then it should represent a valid relationship; assuming you use 0
instead, Doctrine will try to load the association using that, which would of course fail. In these cases you would need to update the database and mapping to allow a null value.
If however you require a default language association to be defined then you explicitly need to set it when you create the entity.
$language = $entityManager->find(1);
$entity = new Entity;
$entity->setLanguage($language);
$entityManager->persist($entity);
$entityManager->flush();
For this reason, you might want to consider a 'service' that encapsulates the creation of your entity so you know a language will always be valid and assigned by default.
Upvotes: 1