skip to Main Content

I have this simple entity for example:

<?php

namespace AppEntityCreator;

use AppRepositoryCreatorActivityContactRepository;
use DoctrineORMMapping as ORM;

#[ORMEntity(repositoryClass: ActivityContactRepository::class)]
#[ORMIndex(columns: ['contact_id'])]
class ActivityContact
{
    #[ORMId]
    #[ORMManyToOne(targetEntity: Activity::class, cascade: ['persist'])]
    private Activity $activity;

    #[ORMId]
    #[ORMColumn(name: 'contact_id', type: 'integer')]
    private int $contactId;

    public function __construct(Activity $activity, int $contactId)
    {
        $this->activity = $activity;
        $this->contactId = $contactId;
    }

    public function getActivity(): Activity
    {
        return $this->activity;
    }

    public function setActivity(Activity $activity): void
    {
        $this->activity = $activity;
    }

    public function getContactId(): int
    {
        return $this->contactId;
    }
}

When I generate a "diff" migration, doctrine automatically creates a query to add a MariaDB INDEX for the column "activity_id".

In my case, this index is not useful and I would like to remove it, without just deleting it from the migration.

Is there a way to specify to not create this index?

Thanks,

4

Answers


  1. Remove attribute #[ORMIndex(...)] above the class name definition. Although you are including that property into an ID of the Entity, the index may be created over the column.

    Login or Signup to reply.
  2. @TEOL I think the only option that you need is to add an SQL query regarding the drop index in the migration file manually. There is no option available in Doctrine.

    Login or Signup to reply.
  3. Short answer: No you can’t.

    An index in activity_id field is being created as you have a ManyToOne relation and a foreign key must be added, so that’s why an index is created, to create the foreign key on it, and you cannot undo this behaviour.

    You will have then one primary index with activity_id and contact_id and another index for activity_id with a foreign key on it.

    Yes, you could use the primary index to hold the foreign key, but doctrine does not use it (as far as I know).

    If you want to get rid of this index, you have to remove the ManyToOne relation. You can create a field called $activity_id, and then you will have to manually fetch the activity entity if you want to retrieve it.

    Don’t worry about the memory used by it, if you have issues with memory, I can assure you that the index created will not be the source of the issues.

    Also, if you are using doctrine migrations, don’t remove the creation of the index in the migrations file, because it will be generated again and again on each diff you generate.

    Login or Signup to reply.
  4. I’m not at home, so i can’t test this, but could you try the following : On the class level, add a @ORMTable and set its indexes to an empty array. Then above your $contactId property, add ORMIndex(columns: [‘contact_id’])

    <?php
    
    namespace AppEntityCreator;
    
    use AppRepositoryCreatorActivityContactRepository;
    use DoctrineORMMapping as ORM;
    
    #[ORMEntity(repositoryClass: ActivityContactRepository::class)]
    #[ORMTable(indexes: [])]
    class ActivityContact
    {
        #[ORMId]
        #[ORMManyToOne(targetEntity: Activity::class, cascade: ['persist'])]
        private Activity $activity;
    
        #[ORMId]
        #[ORMColumn(name: 'contact_id', type: 'integer')]
        #[ORMIndex(columns: ['contact_id'])]
        private int $contactId;
    
        public function __construct(Activity $activity, int $contactId)
        {
            $this->activity = $activity;
            $this->contactId = $contactId;
        }
    
        public function getActivity(): Activity
        {
            return $this->activity;
        }
    
        public function setActivity(Activity $activity): void
        {
            $this->activity = $activity;
        }
    
        public function getContactId(): int
        {
            return $this->contactId;
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search