Strange random names in constraints generated by Doctrine2 (Symfony2)

For some reason, when I generate a migration using

php app/console doctrine:migrations:diff

I get weird names for constraints and indexes like FK_FFE561C6BE3BD8D4 and IDX_FFE561C6BE3BD8D4:

  $ this-> addSql ("ALTER TABLE agent_task ADD agentConfig_id INT UNSIGNED DEFAULT NULL, DROP agent_id");
 $ this-> addSql ("ALTER TABLE agent_task ADD CONSTRAINT FK_FFE561C6BE3BD8D4 FOREIGN KEY (agentConfig_id) REFERENCES agent_config (id)");
 $ this-> addSql ("CREATE INDEX IDX_FFE561C6BE3BD8D4 ON agent_task (agentConfig_id)");

Snippet of object code:

  / **
      * @var AgentConfig
      *
      * @ORM \ ManyToOne (targetEntity = "AgentConfig", inversedBy = "agentTasks")
      * @ORM \ JoinColumn (name = "agent_config_id", referencedColumnName = "id")
      * /
     private $ agentConfig;

Is it possible to define names for them?

UPDATE

I tried indexes, but that didn't help.

  / **
  * AgentTaskConfig
  *
  * @ORM \ Table (name = "agent_task_config", indexes = {@ ORM \ index (name = "agent_task_config_task_id", columns = {"task_id"})})
  * @ORM \ Entity
  * /
 class AgentTaskConfig

Still happening:

  $ this-> addSql ("ALTER TABLE agent_task_config DROP FOREIGN KEY fk_agent_task_id");
 $ this-> addSql ("ALTER TABLE agent_task_config ADD CONSTRAINT FK_7FEDF0EF8DB60186 FOREIGN KEY (task_id) REFERENCES agent_task (id)");
+4
source share
2 answers

Having more time, I researched doctrine (v2.4.x) SchemaTool.php, which generates the generation. It looks like they are using a method:

 Table#addUnnamedForeignKeyConstraint($foreignTable, array $localColumnNames, array $foreignColumnNames, array $options=array()) 

which speaks for itself. It is marked as deprecated with reference to:

 Table#addForeignKeyConstraint($foreignTable, array $localColumnNames, array $foreignColumnNames, array $options=array(), $constraintName = null) 

This method has the argument $ constraintName, which is not currently in use. I think the only way is to edit the Schema tool to use the last method passing your own $ constraintName.

The generation of the constraint name is as follows:

 protected function _generateIdentifierName($columnNames, $prefix='', $maxSize=30) { $hash = implode("", array_map(function($column) { return dechex(crc32($column)); }, $columnNames)); return substr(strtoupper($prefix . "_" . $hash), 0, $maxSize); } 
+6
source

You can define indices on your entities yourself

 /** * * @ORM\Table(name="company", indexes={@ORM\Index(name="model_partner_idx", columns={"partner"})} ) * @ORM\Entity */ class Company 
+4
source

All Articles