5

For some reason when I generate migration using

php app/console doctrine:migrations:diff

I get weird names for constrains & indexes like FK_FFE561C6BE3BD8D4 & 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)");

The entity code snippet:

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

Is there any way to define the names for those?

UPDATE

I tried indexes, but it 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)");

2 Answers 2

8

Having more time I investigated doctrine's (v2.4.x) SchemaTool.php that does the generation. It looks like they are using method:

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

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

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

This method has argument $constraintName, which is not used as of now. I guess the only way is to edit the Schema tool to use the latter method passing your own $constraintName.

The constraint name generation looks like this:

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);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Please advice how is index generation? The same as above? with exception IDX_ instead of prefix?
5

You can define the indexes yourself on your entities

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

2 Comments

yes, but this creates the new ones, but doesn't change names of auto generated indexes
You can use the above annotation to overwrite the auto generated indexes

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.