You are here

public function DataTable::addPrimaryKey in Data 6

Same name and namespace in other branches
  1. 7 includes/DataTable.inc \DataTable::addPrimaryKey()

Add a primary key to table.

Throws

DataException

1 call to DataTable::addPrimaryKey()
DataTable::changePrimaryKey in includes/DataTable.inc
Change the primary keys of a table.

File

includes/DataTable.inc, line 373
Contains class definition for DataTable.

Class

DataTable
Manages data access and manipulation for a single data table. Use data_create_table() or data_get_table() to instantiate an object from this class.

Code

public function addPrimaryKey($fields) {
  $ret = array();
  $schema = $this->table_schema;
  foreach ($fields as $field) {
    if ($schema['fields'][$field]['type'] == 'text') {
      throw new DataException(t('A text field cannot be made a primary key.'));
    }
  }
  db_add_primary_key($ret, $this->name, $fields);
  if ($ret[0]['success']) {
    $schema['primary key'] = $fields;
    $this
      ->update(array(
      'table_schema' => $schema,
    ));
    drupal_get_schema($this->name, TRUE);
    return;
  }
  throw new DataException(t('Error creating primary key.'));
}