function Schema::addUniqueKey

Add a unique key.

Parameters

$table: The table to be altered.

$name: The name of the key.

$fields: An array of field names.

Overrides Schema::addUniqueKey

1 call to Schema::addUniqueKey()
Schema::_createKeys in core/modules/pgsql/src/Driver/Database/pgsql/Schema.php

File

core/modules/pgsql/src/Driver/Database/pgsql/Schema.php, line 810

Class

Schema
PostgreSQL implementation of \Drupal\Core\Database\Schema.

Namespace

Drupal\pgsql\Driver\Database\pgsql

Code

public function addUniqueKey($table, $name, $fields) {
  if (!$this->tableExists($table)) {
    throw new SchemaObjectDoesNotExistException("Cannot add unique key '{$name}' to table '{$table}': table doesn't exist.");
  }
  if ($this->constraintExists($table, $name . '__key')) {
    throw new SchemaObjectExistsException("Cannot add unique key '{$name}' to table '{$table}': unique key already exists.");
  }
  // Use the createPrimaryKeySql(), which already discards any prefix lengths
  // passed as part of the key column specifiers. (Postgres doesn't support
  // setting a prefix length for PRIMARY or UNIQUE indices.)
  $this->connection
    ->query('ALTER TABLE {' . $table . '} ADD CONSTRAINT ' . $this->ensureIdentifiersLength($table, $name, 'key') . ' UNIQUE (' . $this->createPrimaryKeySql($fields) . ')');
  $this->resetTableInformation($table);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.