PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, January 23, 2022

[FIXED] Doctrine rename all my index even if i don't ask for it

 January 23, 2022     doctrine-orm, php, symfony, symfony5     No comments   

Issue

i'm new to doctrine and Symfony, and i don't uderstand why my migration always contains some line for renaming index.

I prefere keeping a readable semantic name for my index, what option should i use ?

This is the command i use for creating migration:

php bin/console make:migration

This is my entity :


/**
 * ArticleFournisseur
 *
 * @ORM\Table(name="article_fournisseur")
 * @ORM\Entity
 */
class ArticleFournisseur
{
    /**
     * @var int|null
     *
     * @ORM\Column(name="Qte", type="integer", nullable=true)
     */
    private $qte;

    /**
     * @var float|null
     *
     * @ORM\Column(name="Prix_unitaire", type="float", precision=10, scale=0, nullable=true)
     */
    private $prixUnitaire;

    /**
     * @var \Article
     *
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="NONE")
     * @ORM\OneToOne(targetEntity="Article")
     * @ORM\JoinColumn(name="no_article", referencedColumnName="no_article")
     */
    private $NoArticle;

    /**
     * @var \Emplacement
     *
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="NONE")
     * @ORM\OneToOne(targetEntity="Emplacement")
     * @ORM\JoinColumn(name="Id_emplacement", referencedColumnName="Id_emplacement")
     */
    private $idEmplacement;

    /**
     * @var \Fournisseur
     *
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="NONE")
     * @ORM\OneToOne(targetEntity="Fournisseur")
     * @ORM\JoinColumn(name="Id_fournisseur", referencedColumnName="Id_fournisseur")
     */
    private $idFournisseur;

    /**
     * @var \Statut
     *
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="NONE")
     * @ORM\ManyToOne(targetEntity="Statut")
     * @ORM\JoinColumn(name="Id_statut", referencedColumnName="Id_Statut")
     */
    private $idStatut;
}

This is part of the result i get in the migration file generated :

// ./migrations/Version202007211565895.php
public function up(Schema $schema) : void
{
    // this up() migration is auto-generated, please modify it to your needs
    $this->addSql('ALTER TABLE article_fournisseur RENAME INDEX fk_art_fou__art TO IDX_AC1DA055BC2E22B2');
    $this->addSql('ALTER TABLE article_fournisseur RENAME INDEX fk_art_fou__emplacement TO IDX_AC1DA05515B9D0D9');
    $this->addSql('ALTER TABLE article_fournisseur RENAME INDEX fk_art_fou__fou TO IDX_AC1DA055E3E87F1D');
    $this->addSql('ALTER TABLE article_fournisseur RENAME INDEX fk_art_fou__statut TO IDX_AC1DA05541A2C7F1');

I just did one thing that is special, i create my entity from my database, by using the following command :

php bin/console doctrine:mapping:import "App\Entity" annotation --path=src/Entity

Thank you for your help

Edit :

Ok, now the line which was renaming index have disapears, but i still got the same problem with name of the foreign key. Please could you take a look at this :

// ./migrations/Version2020072194047.php

    public function up(Schema $schema) : void
    {
        // this up() migration is auto-generated, please modify it to your needs
        $this->addSql('ALTER TABLE article_fournisseur DROP FOREIGN KEY fk_art_fou__statut');
        $this->addSql('ALTER TABLE article_fournisseur ADD CONSTRAINT FK_AC1DA055BC2E22B2 FOREIGN KEY (no_article) REFERENCES article (no_article)');

See, the foreign key constraint is drop, then it's re-create with another name.

But maybe i must have to create an other question for this ?


Solution

Doctrine allows naming indexes (see the docs).

For example:

/**
 * ArticleFournisseur
 *
 * @ORM\Table(name="article_fournisseur",indexes={@Index(name="idx_idEmplacement", columns={"idEmplacement"})})
 * @ORM\Entity
 */

You can adapt this as needed.



Answered By - geoB
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing