Table of Contents

Class ForeignKey

Namespace
YndigoBlue.Velocity.Constraints
Assembly
YndigoBlue.Velocity.dll

Represents a foreign key constraint that enforces referential integrity between tables.

public class ForeignKey : Constraint, IEquatable<Constraint>, IComparable<Constraint>
Inheritance
ForeignKey
Implements

Examples

// Define tables with a foreign key relationship
var customersTable = new Table(schema, "customers");
customersTable.AddColumn("id", DataType.Integer, autoIncrement: true);
customersTable.AddColumn("name", DataType.VarChar, 100);

var ordersTable = new Table(schema, "orders");
ordersTable.AddColumn("id", DataType.Integer, autoIncrement: true);
ordersTable.AddColumn("customer_id", DataType.Integer);
ordersTable.AddColumn("order_date", DataType.Date);

// Create foreign key from orders.customer_id to customers.id
var fk = new ForeignKey(
    "fk_orders_customers",
    schema.Name,
    "orders",
    "customer_id",
    schema.Name,
    "customers",
    "id",
    ForeignKeyRuleType.Cascade,  // ON DELETE CASCADE
    ForeignKeyRuleType.Restrict  // ON UPDATE RESTRICT
);

Remarks

A foreign key constraint ensures that values in one or more columns match values in the referenced (lookup) table's primary key or unique constraint. Supports cascading actions on UPDATE and DELETE operations.

Properties

ConstraintType

Gets the constraint type as ForeignKey.

DeleteRule

Gets the action to take when a row in the referenced table is deleted.

IsComposite

Gets or sets whether this is a composite foreign key (spanning multiple columns).

LookupColumns

Gets or sets the column names in the lookup (referenced) table.

LookupSchema

Gets the schema name of the lookup (referenced) table.

LookupTable

Gets the name of the lookup (referenced) table.

SourceColumns

Gets or sets the column names in the source (referencing) table.

UpdateRule

Gets the action to take when a value in the referenced column is updated.

Methods

GetColumnsForComparison()

Gets columns for comparison in a name-neutral way. Override in subclasses that have columns.

IsEqual(Constraint)

Determines whether this constraint is equal to another constraint based on their properties.