You are here

public function Schema::addPrimaryKey in Drupal driver for SQL Server and SQL Azure 3.0.x

Same name and namespace in other branches
  1. 8.2 drivers/lib/Drupal/Driver/Database/sqlsrv/Schema.php \Drupal\Driver\Database\sqlsrv\Schema::addPrimaryKey()
  2. 8 drivers/lib/Drupal/Driver/Database/sqlsrv/Schema.php \Drupal\Driver\Database\sqlsrv\Schema::addPrimaryKey()

Add a primary key.

Parameters

$table: The table to be altered.

$fields: Fields for the primary key.

Throws

\Drupal\Core\Database\SchemaObjectDoesNotExistException If the specified table doesn't exist.

\Drupal\Core\Database\SchemaObjectExistsException If the specified table already has a primary key.

Overrides Schema::addPrimaryKey

1 call to Schema::addPrimaryKey()
Schema::recreateTableKeys in drivers/lib/Drupal/Driver/Database/sqlsrv/Schema.php
Re-create keys associated to a table.

File

drivers/lib/Drupal/Driver/Database/sqlsrv/Schema.php, line 412

Class

Schema

Namespace

Drupal\Driver\Database\sqlsrv

Code

public function addPrimaryKey($table, $fields) {
  if (!$this
    ->tableExists($table)) {
    throw new SchemaObjectDoesNotExistException(t("Cannot add primary key to table %table: table doesn't exist.", [
      '%table' => $table,
    ]));
  }
  if ($primary_key_name = $this
    ->primaryKeyName($table)) {
    if ($this
      ->isTechnicalPrimaryKey($primary_key_name)) {

      // Destroy the existing technical primary key.
      $this->connection
        ->queryDirect('ALTER TABLE {' . $table . '} DROP CONSTRAINT [' . $primary_key_name . ']');
      $this
        ->resetColumnInformation($table);
      $this
        ->cleanUpTechnicalPrimaryColumn($table);
    }
    else {
      throw new SchemaObjectExistsException(t("Cannot add primary key to table %table: primary key already exists.", [
        '%table' => $table,
      ]));
    }
  }

  // The size limit of the primary key depends on the
  // coexistence with an XML field.
  if ($this
    ->tableHasXmlIndex($table)) {
    $this
      ->createPrimaryKey($table, $fields, self::XML_INDEX_BYTES);
  }
  else {
    $this
      ->createPrimaryKey($table, $fields);
  }
  return TRUE;
}