Returns the foreign key constraint with the given name.

@param string $constraintName The constraint name.

@return ForeignKeyConstraint

@throws SchemaException If the foreign key does not exist.
345943
public function getForeignKey($constraintName)
    {
        $constraintName = $this->normalizeIdentifier($constraintName);
        if (! $this->hasForeignKey($constraintName)) {
            throw SchemaException::foreignKeyDoesNotExist($constraintName, $this->_name);
        }

        return $this->_fkConstraints[$constraintName];
    }
557782
public function withParentIdAs(string $columnName)
    {
        call_user_func($this->callback, function ($idString, Table $parentTable) use ($columnName) {
            /** @var IEntityMapper $mapper */
            $mapper = call_user_func($this->mapperLoader);
            $mapper->addForeignKey(ForeignKey::createWithNamingConvention(
                $mapper->getPrimaryTableName(),
                [$columnName],
                $parentTable->getName(),
                [$parentTable->getPrimaryKeyColumnName()],
                $this->identifying
                    ? ForeignKeyMode::CASCADE
                    : ForeignKeyMode::SET_NULL,
                ForeignKeyMode::CASCADE
            ));

            if ($this->orderPersistColumn) {
                $persistHook = new OrderIndexPropertyLoaderHook(
                    $mapper->getObjectType(),
                    $mapper->getPrimaryTable(),
                    $this->orderPersistColumn,
                    $columnName,
                    $mapper->getDefinition()->getPropertyLinkedToColumn($this->orderPersistColumn)
                );

                $mapper->addPersistHook($persistHook);
                $persistHookId = $persistHook->getIdString();
            } else {
                $persistHookId = null;
            }

            return new ToManyRelation(
                $idString,
                $this->loadIds
                    ? new ToManyRelationIdentityReference($mapper, $this->bidirectionalRelationProperty)
                    : new ToManyRelationObjectReference($mapper, $this->bidirectionalRelationProperty, $persistHookId),
                $columnName,
                $this->identifying
                    ? new IdentifyingRelationMode()
                    : new NonIdentifyingRelationMode(),
                $this->orderByColumnNameDirectionMap,
                $this->orderPersistColumn
            );
        });
    }
206765
public function getFieldConstraints($fieldName)
    {
        $maybeConstraints = $this->annotations->lookupIn(
            [$fieldName, static::ANNOTATION_CONSTRAINTS]
        );

        if ($maybeConstraints->isNothing()) {
            return ArrayList::zero();
        }

        $constraints = Maybe::fromJust($maybeConstraints);

        if (is_array($constraints)) {
            return ArrayList::of($constraints);
        } elseif ($constraints instanceof IterableType) {
            return $constraints;
        }

        return ArrayList::of([$constraints]);
    }
227249
public function getCreateConstraintSQLQueries(ConstraintInterface $constraint, $table)
    {
        if ($constraint instanceof PrimaryKey) {
            return $this->getCreatePrimaryKeySQLQueries($constraint, $table);
        }

        if ($constraint instanceof ForeignKey) {
            return $this->getCreateForeignKeySQLQueries($constraint, $table);
        }

        if ($constraint instanceof Index) {
            return $this->getCreateIndexSQLQueries($constraint, $table);
        }

        if ($constraint instanceof Check) {
            return $this->getCreateCheckSQLQueries($constraint, $table);
        }

        throw PlatformException::constraintNotSupported(get_class($constraint));
    }
1136023
public function getTable($tableName)
    {
        $table = new Table($tableName);

        $columns = $this->_getColumns($tableName);
        foreach ($columns as $col) {
            $table->addColumn($this->_createColumn($col));
        }

        $constraints = $this->_getConstraints($tableName);
        foreach ($constraints as $constraint) {
            $object = $this->_createConstraint($constraint);
            if ($object) {
                $table->addConstraint($object);
            }
        }

        return $table;
    }
891846
protected function getTableConstraints($tableName)
	{
		$this->connect();

		$this->setQuery(
			'SELECT CONSTRAINT_NAME FROM' . ' INFORMATION_SCHEMA.TABLE_CONSTRAINTS' . ' WHERE TABLE_NAME = ' . $this->quote($tableName)
		);

		return $this->loadColumn();
	}
81031
public function getPrimaryKey($tableName)
    {
        $rows = $this->fetchAll(sprintf(
            "SELECT
                    tc.constraint_name,
                    kcu.column_name
                FROM information_schema.table_constraints AS tc
                JOIN information_schema.key_column_usage AS kcu
                    ON tc.constraint_name = kcu.constraint_name
                WHERE constraint_type = 'PRIMARY KEY'
                    AND tc.table_name = '%s'
                ORDER BY kcu.ordinal_position",
            $tableName
        ));

        $primaryKey = [
            'columns' => [],
        ];
        foreach ($rows as $row) {
            $primaryKey['constraint'] = $row['constraint_name'];
            $primaryKey['columns'][] = $row['column_name'];
        }

        return $primaryKey;
    }
1368889
public FKeyModel getFkey(String fkeyName) {
		if (fkeyConstraints == null)
			return null;
		for (FKeyModel fkey : fkeyConstraints)
			if (!StrUtils.isEmpty(fkeyName) && fkeyName.equalsIgnoreCase(fkey.getFkeyName()))
				return fkey;
		return null;
	}
540699
public function fieldIsRequired($fieldName)
    {
        $required = false;

        $constraints = $this->getConstraints($fieldName);
        if ($constraints) {
            foreach ($constraints as $constraint) {
                if ($constraint instanceof Constraint_required) {
                    $required = true;
                    break;
                }
            }
        }

        return $required || parent::fieldIsRequired($fieldName);
    }
351404
def get_foreign_key(self, name):
        
        name = self._normalize_identifier(name)

        if not self.has_foreign_key(name):
            raise ForeignKeyDoesNotExist(name, self._name)

        return self._fk_constraints[name]
354472
public function getCreateForeignKeySQL(ForeignKeyConstraint $foreignKey, $table)
    {
        if ($table instanceof Table) {
            $table = $table->getQuotedName($this);
        }

        return 'ALTER TABLE ' . $table . ' ADD ' . $this->getForeignKeyDeclarationSQL($foreignKey);
    }
722078
public function prepareHasAndBelongsToManyConstraintStatement($type_name, $connection_table, $constraint_name, $field_name)
    {
        $result = [];

        $result[] = 'ALTER TABLE ' . $this->getConnection()->escapeTableName($connection_table);
        $result[] = '    ADD CONSTRAINT ' . $this->getConnection()->escapeFieldName($constraint_name);
        $result[] = '    FOREIGN KEY (' . $field_name . ') REFERENCES ' . $this->getConnection()->escapeTableName($type_name) . '(`id`)';
        $result[] = '    ON UPDATE CASCADE ON DELETE CASCADE;';

        return implode("\n", $result);
    }
1289989
protected function _getConstraints($tableName)
    {

        $params = [
            ':schemaName' => $this->_adapter->getSchemaName(),
            ':tableName' => $tableName
        ];

        return $this->_adapter->query($this->_getConstraintsSql, $params);
    }
1201202
public static function getSchema($schemaName)
    {
        if (!isset(self::$schemas[$schemaName])) {
            if (!isset(self::$schemaClasses[$schemaName])) {
                throw new SchemaNotFoundException($schemaName);
            }

            $schemaClass = self::$schemaClasses[$schemaName];
            $schema = new $schemaClass();

            if (!($schema instanceof SolutionSchema)) {
                throw new SchemaRegistrationException($schemaName, $schemaClass);
            }

            self::$schemas[$schemaName] = $schema;

            $schema->defineRelationships();
        }

        return self::$schemas[$schemaName];
    }
533438
public function on(string $referencedTableName) : ForeignKeyOnDeleteModeDefiner
    {
        return new ForeignKeyOnDeleteModeDefiner(
                $this->callback,
                $this->localColumnNames,
                $this->referencedColumnNames,
                $referencedTableName
        );
    }
1167949
public function setConstraint($fieldName, $constraint)
    {
        $class = get_class($constraint);
        // remove existing constraint if it already exists
        if ($this->getConstraint($fieldName, $class)) {
            $this->removeConstraint($fieldName, $class);
        }

        $this->constraints[$fieldName][$class] = $constraint;

        if ($this->form) {
            $dataField = $this->form->Fields()->dataFieldByName($fieldName);
            $constraint->setField($dataField);
            if ($this->parsleyEnabled) {
                // If there is no field, output a clear error message before trying to apply parsley
                if (!$dataField) {
                    throw new Exception("You have set a constraint on '$fieldName' but it does not exist in the FieldList.");
                }
                $constraint->applyParsley();
            }
        }

        return $this;
    }
1007879
protected function getForeignKeys($tableName)
    {
        $parts = $this->getSchemaName($tableName);
        $foreignKeys = [];
        $rows = $this->fetchAll(sprintf(
            "SELECT
                    tc.constraint_name,
                    tc.table_name, kcu.column_name,
                    ccu.table_name AS referenced_table_name,
                    ccu.column_name AS referenced_column_name
                FROM
                    information_schema.table_constraints AS tc
                    JOIN information_schema.key_column_usage AS kcu ON tc.constraint_name = kcu.constraint_name
                    JOIN information_schema.constraint_column_usage AS ccu ON ccu.constraint_name = tc.constraint_name
                WHERE constraint_type = 'FOREIGN KEY' AND tc.table_schema = %s AND tc.table_name = %s
                ORDER BY kcu.position_in_unique_constraint",
            $this->getConnection()->quote($parts['schema']),
            $this->getConnection()->quote($parts['table'])
        ));
        foreach ($rows as $row) {
            $foreignKeys[$row['constraint_name']]['table'] = $row['table_name'];
            $foreignKeys[$row['constraint_name']]['columns'][] = $row['column_name'];
            $foreignKeys[$row['constraint_name']]['referenced_table'] = $row['referenced_table_name'];
            $foreignKeys[$row['constraint_name']]['referenced_columns'][] = $row['referenced_column_name'];
        }

        return $foreignKeys;
    }