final class ForeignKey
    implements TableElement
    uses Snapshot
Public API: explicitly declared with @visibility public.

A FOREIGN KEY constraint: the child columns and the reference to the parent table.

Rule: MYSQL-FOREIGN-KEY-001. The grammar admits prefix lengths and directions on the child columns; they are kept as written. The name after FOREIGN KEY names the index the server creates for the constraint. The parent table is resolved by MYSQL-REFERENCES-001. Source: https://dev.mysql.com/doc/refman/8.4/en/create-table-foreign-keys.html. Status: Implemented.

Reading a foreign keydoctest
$create = (new \SqlSemantics\Platform\MySql\Semantics())->analyze('CREATE TABLE c (p INT, CONSTRAINT fk FOREIGN KEY (p) REFERENCES p (id))');
    [$create->statement->elements[1]->constraint->name->column->value, $create->statement->elements[1]->references->table->name->value] // => ['fk', 'p']

Properties§

public non-empty-list<ColumnPart> $columns
public References $references
public ColumnName|null $name = null
public ConstraintName|null $constraint = null

Methods§

public function __construct(
    list<ColumnPart> $columns,
    public References $references,
    public ColumnName|null $name = null,
    public ConstraintName|null $constraint = null,
)

Parameters

$columnslist<ColumnPart>The child columns in order; at least one
$referencesReferencesThe parent table and columns
$nameColumnName|nullThe index name written after FOREIGN KEY
$constraintConstraintName|nullThe CONSTRAINT clause, when written
Calls 2
public function deriveElement(Derivation $derivation, Environment $scope): void

Derives nothing: the child columns are names, and the parent table is resolved by the statement.

Parameters

$derivationDerivation
$scopeEnvironment
public function render(Output $out): void

Writes the constraint, the columns and the reference.

Parameters

$outOutput
Calls 1

Test cases 4§

Test cases that cover or call this symbol, from the coverage report and from the analyzed test sources.

Dedicated tests 1
Other tests reaching this symbol 3

Relations§

Instantiated in 1
Type checks 2