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
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.
Examples
using (var manager = new Manager(connection))
{
var schema = manager.LoadSchema("myschema");
// Single-column foreign key: orders.customer_id -> customers.id
Table ordersTable = schema.CreateTable("orders");
ordersTable.CreateColumn("id", DataType.Integer, autoGenerate: true);
ordersTable.CreateColumn("customer_id", DataType.Integer);
ordersTable.CreateColumn("order_date", DataType.Date);
ForeignKey fk = ordersTable.CreateForeignKey(
"fk_orders_customers",
"customer_id", // source column
"myschema", // lookup schema
"customers", // lookup table
"id", // lookup column
ForeignKeyRuleType.Cascade, // ON DELETE
ForeignKeyRuleType.Restrict // ON UPDATE
);
manager.CreateTable(ordersTable);
manager.CreateConstraint(fk);
// Composite foreign key across multiple columns
Table orderItemsTable = schema.CreateTable("order_items");
orderItemsTable.CreateColumn("order_id", DataType.Integer);
orderItemsTable.CreateColumn("product_id", DataType.Integer);
ForeignKey compositeFk = orderItemsTable.CreateForeignKey(
"fk_order_items_products",
[ "order_id", "product_id" ],
"myschema",
"products",
[ "order_id", "product_id" ],
ForeignKeyRuleType.Cascade,
ForeignKeyRuleType.Cascade
);
manager.CreateTable(orderItemsTable);
manager.CreateConstraint(compositeFk);
}
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 whether this is a composite foreign key (spanning multiple columns).
- LookupColumns
Gets 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 the column names in the source (referencing) table.
- UpdateRule
Gets the action to take when a value in the referenced column is updated.
Methods
- IsEqual(Constraint)
Determines whether this constraint is equal to another constraint based on their properties.