classForeignKey
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.
$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> $columnspublic References $referencespublic ColumnName|null $name = nullpublic ConstraintName|null $constraint = nullMethods§
public function __construct(
list<ColumnPart> $columns,
public References $references,
public ColumnName|null $name = null,
public ConstraintName|null $constraint = null,
)Parameters
$columns | list<ColumnPart> | The child columns in order; at least one |
$references | References | The parent table and columns |
$name | ColumnName|null | The index name written after FOREIGN KEY |
$constraint | ConstraintName|null | The CONSTRAINT clause, when written |
Calls 2
- static-call Check::listOf() line 46
- class-const ColumnPart::class() line 46
public function deriveElement(Derivation $derivation, Environment $scope): voidDerives nothing: the child columns are names, and the parent table is resolved by the statement.
Parameters
$derivation | Derivation | |
$scope | Environment |
public function render(Output $out): voidWrites the constraint, the columns and the reference.
Parameters
$out | Output |
Calls 1
- method-call Output::node() line 61
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
- instanceof SqlSemantics\Platform\MySql\Rules\TableDefinition\ElementFacts::element() packages/sql-semantics-mysql/src/Rules/TableDefinition/ElementFacts.php:48
- instanceof SqlSemantics\Platform\MySql\Rules\TableDefinition\TableProblems::report() packages/sql-semantics-mysql/src/Rules/TableDefinition/TableProblems.php:73