# ConstraintFinderTrait

> ConstraintFinderTrait provides methods for getting a table constraint information.

[API reference](/docs/api/index.md) › [yii](/docs/api/yii/index.md) › [db](/docs/api/yii/db/index.md) › ConstraintFinderTrait

| trait | `yii\db\ConstraintFinderTrait` |
| --- | --- |
| Since | 2.0.13 |
| Source | [`framework/db/ConstraintFinderTrait.php`](https://github.com/yiisoft/yii2/blob/22.0/framework/db/ConstraintFinderTrait.php#L30) |

Used by 5 types:

- [`yii\db\mssql\Schema`](/docs/api/yii/db/mssql/schema/index.md)
- [`yii\db\mysql\Schema`](/docs/api/yii/db/mysql/schema/index.md)
- [`yii\db\oci\Schema`](/docs/api/yii/db/oci/schema/index.md)
- [`yii\db\pgsql\Schema`](/docs/api/yii/db/pgsql/schema/index.md)
- [`yii\db\sqlite\Schema`](/docs/api/yii/db/sqlite/schema/index.md)

ConstraintFinderTrait provides methods for getting a table constraint information.

## Properties

| Property | Type | Description | Defined by |
| --- | --- | --- | --- |
| [`$schemaChecks`](#property-schemaChecks) | [`CheckConstraint`](/docs/api/yii/db/checkconstraint/index.md)`[][]` | Check constraints for all tables in the database. | `ConstraintFinderTrait` |
| [`$schemaDefaultValues`](#property-schemaDefaultValues) | [`DefaultValueConstraint`](/docs/api/yii/db/defaultvalueconstraint/index.md)`[]` | Default value constraints for all tables in the database. | `ConstraintFinderTrait` |
| [`$schemaForeignKeys`](#property-schemaForeignKeys) | [`ForeignKeyConstraint`](/docs/api/yii/db/foreignkeyconstraint/index.md)`[][]` | Foreign keys for all tables in the database. | `ConstraintFinderTrait` |
| [`$schemaIndexes`](#property-schemaIndexes) | [`IndexConstraint`](/docs/api/yii/db/indexconstraint/index.md)`[][]` | Indexes for all tables in the database. | `ConstraintFinderTrait` |
| [`$schemaPrimaryKeys`](#property-schemaPrimaryKeys) | [`Constraint`](/docs/api/yii/db/constraint/index.md)`[]` | Primary keys for all tables in the database. | `ConstraintFinderTrait` |
| [`$schemaUniques`](#property-schemaUniques) | [`IndexConstraint`](/docs/api/yii/db/indexconstraint/index.md)`[][]` | Unique constraints for all tables in the database. | `ConstraintFinderTrait` |

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| [`getSchemaChecks()`](#method-getSchemaChecks) | Returns check constraints for all tables in the database. | `ConstraintFinderTrait` |
| [`getSchemaDefaultValues()`](#method-getSchemaDefaultValues) | Returns default value constraints for all tables in the database. | `ConstraintFinderTrait` |
| [`getSchemaForeignKeys()`](#method-getSchemaForeignKeys) | Returns foreign keys for all tables in the database. | `ConstraintFinderTrait` |
| [`getSchemaIndexes()`](#method-getSchemaIndexes) | Returns indexes for all tables in the database. | `ConstraintFinderTrait` |
| [`getSchemaPrimaryKeys()`](#method-getSchemaPrimaryKeys) | Returns primary keys for all tables in the database. | `ConstraintFinderTrait` |
| [`getSchemaUniques()`](#method-getSchemaUniques) | Returns unique constraints for all tables in the database. | `ConstraintFinderTrait` |
| [`getTableChecks()`](#method-getTableChecks) | Obtains the check constraints information for the named table. | `ConstraintFinderTrait` |
| [`getTableDefaultValues()`](#method-getTableDefaultValues) | Obtains the default value constraints information for the named table. | `ConstraintFinderTrait` |
| [`getTableForeignKeys()`](#method-getTableForeignKeys) | Obtains the foreign keys information for the named table. | `ConstraintFinderTrait` |
| [`getTableIndexes()`](#method-getTableIndexes) | Obtains the indexes information for the named table. | `ConstraintFinderTrait` |
| [`getTablePrimaryKey()`](#method-getTablePrimaryKey) | Obtains the primary key for the named table. | `ConstraintFinderTrait` |
| [`getTableUniques()`](#method-getTableUniques) | Obtains the unique constraints information for the named table. | `ConstraintFinderTrait` |
| [`getSchemaMetadata()`](#method-getSchemaMetadata) protected abstract | Returns the metadata of the given type for all tables in the given schema. | `ConstraintFinderTrait` |
| [`getTableMetadata()`](#method-getTableMetadata) protected abstract | Returns the metadata of the given type for the given table. | `ConstraintFinderTrait` |
| [`loadTableChecks()`](#method-loadTableChecks) protected abstract | Loads all check constraints for the given table. | `ConstraintFinderTrait` |
| [`loadTableDefaultValues()`](#method-loadTableDefaultValues) protected abstract | Loads all default value constraints for the given table. | `ConstraintFinderTrait` |
| [`loadTableForeignKeys()`](#method-loadTableForeignKeys) protected abstract | Loads all foreign keys for the given table. | `ConstraintFinderTrait` |
| [`loadTableIndexes()`](#method-loadTableIndexes) protected abstract | Loads all indexes for the given table. | `ConstraintFinderTrait` |
| [`loadTablePrimaryKey()`](#method-loadTablePrimaryKey) protected abstract | Loads a primary key for the given table. | `ConstraintFinderTrait` |
| [`loadTableUniques()`](#method-loadTableUniques) protected abstract | Loads all unique constraints for the given table. | `ConstraintFinderTrait` |

## Property details

### $schemaChecks <span id="property-schemaChecks"></span>

```php
public yii\db\CheckConstraint[][] $schemaChecks
```

Check constraints for all tables in the database.

Each array element is an array of [`CheckConstraint`](/docs/api/yii/db/checkconstraint/index.md) or its child classes. This property is read-only.

### $schemaDefaultValues <span id="property-schemaDefaultValues"></span>

```php
public yii\db\DefaultValueConstraint[] $schemaDefaultValues
```

Default value constraints for all tables in the database.

Each array element is an array of [`DefaultValueConstraint`](/docs/api/yii/db/defaultvalueconstraint/index.md) or its child classes. This property is read-only.

### $schemaForeignKeys <span id="property-schemaForeignKeys"></span>

```php
public yii\db\ForeignKeyConstraint[][] $schemaForeignKeys
```

Foreign keys for all tables in the database.

Each
array element is an array of [`ForeignKeyConstraint`](/docs/api/yii/db/foreignkeyconstraint/index.md) or its child classes. This property is read-only.

### $schemaIndexes <span id="property-schemaIndexes"></span>

```php
public yii\db\IndexConstraint[][] $schemaIndexes
```

Indexes for all tables in the database.

Each array element is
an array of [`IndexConstraint`](/docs/api/yii/db/indexconstraint/index.md) or its child classes. This property is read-only.

### $schemaPrimaryKeys <span id="property-schemaPrimaryKeys"></span>

```php
public yii\db\Constraint[] $schemaPrimaryKeys
```

Primary keys for all tables in the database.

Each array element
is an instance of [`Constraint`](/docs/api/yii/db/constraint/index.md) or its child class. This property is read-only.

### $schemaUniques <span id="property-schemaUniques"></span>

```php
public yii\db\IndexConstraint[][] $schemaUniques
```

Unique constraints for all tables in the database.

Each array element is an array of [`IndexConstraint`](/docs/api/yii/db/indexconstraint/index.md) or its child classes. This property is read-only.

## Method details

### getSchemaChecks() <span id="method-getSchemaChecks"></span>

```php
public function getSchemaChecks(string $schema = '', bool $refresh = false): yii\db\CheckConstraint[][]
```

Returns check constraints for all tables in the database.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$schema` | `string` | The schema of the tables. Defaults to empty string, meaning the current or default schema name. |
| `$refresh` | `bool` | Whether to fetch the latest available table schemas. If this is false, cached data may be returned if available. |

**Returns**

[`CheckConstraint`](/docs/api/yii/db/checkconstraint/index.md)`[][]`: Check constraints for all tables in the database. Each array element is an array of [`CheckConstraint`](/docs/api/yii/db/checkconstraint/index.md) or its child classes.

### getSchemaDefaultValues() <span id="method-getSchemaDefaultValues"></span>

```php
public function getSchemaDefaultValues(string $schema = '', bool $refresh = false): yii\db\DefaultValueConstraint[]
```

Returns default value constraints for all tables in the database.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$schema` | `string` | The schema of the tables. Defaults to empty string, meaning the current or default schema name. |
| `$refresh` | `bool` | Whether to fetch the latest available table schemas. If this is false, cached data may be returned if available. |

**Returns**

[`DefaultValueConstraint`](/docs/api/yii/db/defaultvalueconstraint/index.md)`[]`: Default value constraints for all tables in the database. Each array element is an array of [`DefaultValueConstraint`](/docs/api/yii/db/defaultvalueconstraint/index.md) or its child classes.

### getSchemaForeignKeys() <span id="method-getSchemaForeignKeys"></span>

```php
public function getSchemaForeignKeys(string $schema = '', bool $refresh = false): yii\db\ForeignKeyConstraint[][]
```

Returns foreign keys for all tables in the database.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$schema` | `string` | The schema of the tables. Defaults to empty string, meaning the current or default schema name. |
| `$refresh` | `bool` | Whether to fetch the latest available table schemas. If this is false, cached data may be returned if available. |

**Returns**

[`ForeignKeyConstraint`](/docs/api/yii/db/foreignkeyconstraint/index.md)`[][]`: Foreign keys for all tables in the database. Each array element is an array of [`ForeignKeyConstraint`](/docs/api/yii/db/foreignkeyconstraint/index.md) or its child classes.

### getSchemaIndexes() <span id="method-getSchemaIndexes"></span>

```php
public function getSchemaIndexes(string $schema = '', bool $refresh = false): yii\db\IndexConstraint[][]
```

Returns indexes for all tables in the database.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$schema` | `string` | The schema of the tables. Defaults to empty string, meaning the current or default schema name. |
| `$refresh` | `bool` | Whether to fetch the latest available table schemas. If this is false, cached data may be returned if available. |

**Returns**

[`IndexConstraint`](/docs/api/yii/db/indexconstraint/index.md)`[][]`: Indexes for all tables in the database. Each array element is an array of [`IndexConstraint`](/docs/api/yii/db/indexconstraint/index.md) or its child classes.

### getSchemaPrimaryKeys() <span id="method-getSchemaPrimaryKeys"></span>

```php
public function getSchemaPrimaryKeys(string $schema = '', bool $refresh = false): yii\db\Constraint[]
```

Returns primary keys for all tables in the database.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$schema` | `string` | The schema of the tables. Defaults to empty string, meaning the current or default schema name. |
| `$refresh` | `bool` | Whether to fetch the latest available table schemas. If this is `false`, cached data may be returned if available. |

**Returns**

[`Constraint`](/docs/api/yii/db/constraint/index.md)`[]`: Primary keys for all tables in the database. Each array element is an instance of [`Constraint`](/docs/api/yii/db/constraint/index.md) or its child class.

### getSchemaUniques() <span id="method-getSchemaUniques"></span>

```php
public function getSchemaUniques(string $schema = '', bool $refresh = false): yii\db\Constraint[][]
```

Returns unique constraints for all tables in the database.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$schema` | `string` | The schema of the tables. Defaults to empty string, meaning the current or default schema name. |
| `$refresh` | `bool` | Whether to fetch the latest available table schemas. If this is false, cached data may be returned if available. |

**Returns**

[`Constraint`](/docs/api/yii/db/constraint/index.md)`[][]`: Unique constraints for all tables in the database. Each array element is an array of [`Constraint`](/docs/api/yii/db/constraint/index.md) or its child classes.

### getTableChecks() <span id="method-getTableChecks"></span>

```php
public function getTableChecks(string $name, bool $refresh = false): yii\db\CheckConstraint[]
```

Obtains the check constraints information for the named table.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$name` | `string` | Table name. The table name may contain schema name if any. Do not quote the table name. |
| `$refresh` | `bool` | Whether to reload the information even if it is found in the cache. |

**Returns**

[`CheckConstraint`](/docs/api/yii/db/checkconstraint/index.md)`[]`: Table check constraints.

### getTableDefaultValues() <span id="method-getTableDefaultValues"></span>

```php
public function getTableDefaultValues(string $name, bool $refresh = false): yii\db\DefaultValueConstraint[]
```

Obtains the default value constraints information for the named table.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$name` | `string` | Table name. The table name may contain schema name if any. Do not quote the table name. |
| `$refresh` | `bool` | Whether to reload the information even if it is found in the cache. |

**Returns**

[`DefaultValueConstraint`](/docs/api/yii/db/defaultvalueconstraint/index.md)`[]`: Table default value constraints.

### getTableForeignKeys() <span id="method-getTableForeignKeys"></span>

```php
public function getTableForeignKeys(string $name, bool $refresh = false): yii\db\ForeignKeyConstraint[]
```

Obtains the foreign keys information for the named table.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$name` | `string` | Table name. The table name may contain schema name if any. Do not quote the table name. |
| `$refresh` | `bool` | Whether to reload the information even if it is found in the cache. |

**Returns**

[`ForeignKeyConstraint`](/docs/api/yii/db/foreignkeyconstraint/index.md)`[]`: Table foreign keys.

### getTableIndexes() <span id="method-getTableIndexes"></span>

```php
public function getTableIndexes(string $name, bool $refresh = false): yii\db\IndexConstraint[]
```

Obtains the indexes information for the named table.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$name` | `string` | Table name. The table name may contain schema name if any. Do not quote the table name. |
| `$refresh` | `bool` | Whether to reload the information even if it is found in the cache. |

**Returns**

[`IndexConstraint`](/docs/api/yii/db/indexconstraint/index.md)`[]`: Table indexes.

### getTablePrimaryKey() <span id="method-getTablePrimaryKey"></span>

```php
public function getTablePrimaryKey(string $name, bool $refresh = false): yii\db\Constraint|null
```

Obtains the primary key for the named table.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$name` | `string` | Table name. The table name may contain schema name if any. Do not quote the table name. |
| `$refresh` | `bool` | Whether to reload the information even if it is found in the cache. |

**Returns**

[`Constraint`](/docs/api/yii/db/constraint/index.md)`|null`: Table primary key, `null` if the table has no primary key.

### getTableUniques() <span id="method-getTableUniques"></span>

```php
public function getTableUniques(string $name, bool $refresh = false): yii\db\Constraint[]
```

Obtains the unique constraints information for the named table.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$name` | `string` | Table name. The table name may contain schema name if any. Do not quote the table name. |
| `$refresh` | `bool` | Whether to reload the information even if it is found in the cache. |

**Returns**

[`Constraint`](/docs/api/yii/db/constraint/index.md)`[]`: Table unique constraints.

### getSchemaMetadata() <span id="method-getSchemaMetadata"></span>

```php
abstract protected function getSchemaMetadata(string $schema, string $type, bool $refresh): array
```

Returns the metadata of the given type for all tables in the given schema.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$schema` | `string` | The schema of the metadata. Defaults to empty string, meaning the current or default schema name. |
| `$type` | `string` | Metadata type. |
| `$refresh` | `bool` | Whether to fetch the latest available table metadata. If this is `false`, cached data may be returned if available. |

**Returns**

`array`: Array of metadata.

### getTableMetadata() <span id="method-getTableMetadata"></span>

```php
abstract protected function getTableMetadata(string $name, string $type, bool $refresh): mixed
```

Returns the metadata of the given type for the given table.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$name` | `string` | Table name. The table name may contain schema name if any. Do not quote the table name. |
| `$type` | `string` | Metadata type. |
| `$refresh` | `bool` | Whether to reload the table metadata even if it is found in the cache. |

**Returns**

`mixed`: Metadata.

### loadTableChecks() <span id="method-loadTableChecks"></span>

```php
abstract protected function loadTableChecks(string $tableName): yii\db\CheckConstraint[]
```

Loads all check constraints for the given table.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$tableName` | `string` | Table name. |

**Returns**

[`CheckConstraint`](/docs/api/yii/db/checkconstraint/index.md)`[]`: Check constraints for the given table.

### loadTableDefaultValues() <span id="method-loadTableDefaultValues"></span>

```php
abstract protected function loadTableDefaultValues(string $tableName): yii\db\DefaultValueConstraint[]
```

Loads all default value constraints for the given table.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$tableName` | `string` | Table name. |

**Returns**

[`DefaultValueConstraint`](/docs/api/yii/db/defaultvalueconstraint/index.md)`[]`: Default value constraints for the given table.

### loadTableForeignKeys() <span id="method-loadTableForeignKeys"></span>

```php
abstract protected function loadTableForeignKeys(string $tableName): yii\db\ForeignKeyConstraint[]
```

Loads all foreign keys for the given table.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$tableName` | `string` | Table name. |

**Returns**

[`ForeignKeyConstraint`](/docs/api/yii/db/foreignkeyconstraint/index.md)`[]`: Foreign keys for the given table.

### loadTableIndexes() <span id="method-loadTableIndexes"></span>

```php
abstract protected function loadTableIndexes(string $tableName): yii\db\IndexConstraint[]
```

Loads all indexes for the given table.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$tableName` | `string` | Table name. |

**Returns**

[`IndexConstraint`](/docs/api/yii/db/indexconstraint/index.md)`[]`: Indexes for the given table.

### loadTablePrimaryKey() <span id="method-loadTablePrimaryKey"></span>

```php
abstract protected function loadTablePrimaryKey(string $tableName): yii\db\Constraint|null
```

Loads a primary key for the given table.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$tableName` | `string` | Table name. |

**Returns**

[`Constraint`](/docs/api/yii/db/constraint/index.md)`|null`: Primary key for the given table, `null` if the table has no primary key.

### loadTableUniques() <span id="method-loadTableUniques"></span>

```php
abstract protected function loadTableUniques(string $tableName): yii\db\Constraint[]
```

Loads all unique constraints for the given table.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$tableName` | `string` | Table name. |

**Returns**

[`Constraint`](/docs/api/yii/db/constraint/index.md)`[]`: Unique constraints for the given table.