Question

Doctrine Automatically creates indexes on columns that are used to define object relations,

For example

user: id, name

message: id, sender_id, receiver_id, message

if I define relationship between message and user in a way that message has one Sender and has one Receiver, doctrine will automatically index sender_id and receiver_id fields when I generate sql from model. I would like to disable index on sender, because I manually create index with sender_id and receiver id together. How can I disable auto generated index?

Was it helpful?

Solution

Hello I assumed you were using MySQL, and took a look in Doctrine/Export/Mysql.php I found this :

// build indexes for all foreign key fields (needed in MySQL!!)
if (isset($options['foreignKeys'])) {
      foreach ($options['foreignKeys'] as $fk) {
          $local = $fk['local'];
          $found = false;
          if (isset($options['indexes'])) {
             foreach ($options['indexes'] as $definition) {
                 if (is_string($definition['fields'])) {
                     // Check if index already exists on the column                            
                   $found = $found || ($local == $definition['fields']);    
                 } else if (in_array($local, $definition['fields']) && count($definition['fields']) === 1) {
                    // Index already exists on the column
                    $found = true;
                 }   
            }   
        }   
        if (isset($options['primary']) && !empty($options['primary']) &&
                in_array($local, $options['primary'])) {
             // field is part of the PK and therefore already indexed
             $found = true;
        }   

        if ( ! $found) {
            if (is_array($local)) {
             foreach($local as $localidx) {
                 $options['indexes'][$localidx] = array('fields' => array($localidx => array()));
             }   
        } else {
              $options['indexes'][$local] = array('fields' => array($local => array()));    
             }   
         }   
     }   
 }   

If I understand correctly, to disable the index should be part of the primary key.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top