FK + CHECK constraints.
A constraint checks whether a write is valid. A foreign key checks a reference to another record; a CHECK constraint tests a condition such as a nonnegative amount. Define the rules with the schema, then test both an accepted and a rejected write.
- Define the ruleChoose a primary key, foreign key or CHECK constraint.
- Prepare referenced recordsInsert parent rows before dependent rows.
- Try valid and invalid valuesUse a small sample to exercise the rule.
- Inspect acceptance and rejectionCorrect invalid data instead of blindly repeating the write.
Foreign keys.
The example links a child record to a parent key. Insert the parent before writing a child that references it.
The effect of deleting a parent depends on its declared action and the deployed engine’s supported actions. Restricting a deletion, setting a reference to NULL and cascading a deletion are different choices. Check the schema reference and test the selected action before using it on live data.
# manifest.toml
namespace = "shop"
table = "orders"
primary_key = ["id"]
# ... [[columns]] declarations ...
[[foreign_keys]]
name = "fk_client"
from_col = "client_id"
target_schema = "shop.clients"
target_col = "id"
on_delete = "restrict" # no_action | restrict | set_nullCHECK expressions.
Use a CHECK expression for a condition on the stored values. The supported expression subset includes comparisons, boolean combinations, NULL checks and membership tests. Unsupported expressions need a different rule or application validation.
[[check_constraints]]
name = "amount_positive"
expression = "amount >= 0 AND status IN ('pending', 'paid', 'shipped')"
[[check_constraints]]
name = "refund_window"
expression = "refunded_at IS NULL OR refunded_at >= shipped_at"3-valued logic.
SQL conditions can evaluate to true, false or unknown. A CHECK rejects false; an unknown result caused by NULL can pass. If a value must exist, use the required-field rule or an explicit IS NOT NULL condition.
Do not assume every expression containing NULL becomes unknown: boolean combinations such as OR can still determine a true result.
Errors.
Read the returned status and constraint detail. Fix the invalid value or missing parent before trying again. For a batch, inspect its documented transaction boundary; a constraint failure does not imply that every earlier operation in a larger import was rolled back.
See write behavior and error handling.