Welcome Guest, Not a member yet? Register   Sign In
db_forge->add_key does not create multiple-column indexes
#1

[eluser]vrencianz[/eluser]
The following lines

Code:
$this->dbforge->add_key(array(array('status', 'created'), array('status', 'title')));

Code:
$this->dbforge->add_key(array('status', 'created'));
$this->dbforge->add_key(array('status', 'title'));

generates the following error:

Code:
Duplicate key name 'status'

... , KEY `status` (`status`), KEY `created` (`created`), KEY `status` (`status`), KEY `title` (`title`) )

The problem is the db_forge function

Code:
function add_key($key = '', $primary = FALSE)
{
  if (is_array($key))
  {
   foreach ($key as $one)
   {
    $this->add_key($one, $primary);
   }

   return;
  }

  if ($key == '')
  {
   show_error('Key information is required for that operation.');
  }

  if ($primary === TRUE)
  {
   $this->primary_keys[] = $key;
  }
  else
  {
   $this->keys[] = $key;
  }
}

which explodes recursively any array when mysql _create_table expects arrays for multiple-column indexes.

Code:
if (is_array($keys) && count($keys) > 0)
  {
   foreach ($keys as $key)
   {
    if (is_array($key))
    {
     $key_name = $this->db->_protect_identifiers(implode('_', $key));
     $key = $this->db->_protect_identifiers($key);
    }
    else
    {
     $key_name = $this->db->_protect_identifiers($key);
     $key = array($key_name);
    }

    $sql .= ",\n\tKEY {$key_name} (" . implode(', ', $key) . ")";
   }
  }

Thank you!
#2

[eluser]InsiteFX[/eluser]
Maybe because you are using the same name status for both keys.

You can only have one key named status!
#3

[eluser]vrencianz[/eluser]
Thank you for the fast reply

According to the documentation

Code:
$this->dbforge->add_key(array('blog_name', 'blog_label'));
// gives KEY `blog_name_blog_label` (`blog_name`, `blog_label`)

the expected keys are (also the _create_table wants to do that)

Code:
$this->dbforge->add_key(array('status', 'created'));
$this->dbforge->add_key(array('status', 'title'));  

// KEY `status_created` (`status`, `created`)
// KEY `status_title` (`status`, `title`)

Please observe (as the first post lists it) that the add_key reduces any array to its keys (and sub-keys if the key is an array) and for multiple-column keys imploded-underscored field names are used.
#4

[eluser]vrencianz[/eluser]
It seams that this one is fixed here: https://github.com/harryxu/CodeIgniter/c...76c7d4fb34. Thanks!




Theme © iAndrew 2016 - Forum software by © MyBB