Constraints
Invariant support is currently limited to referential integrity and not-null constraints. Unique and check constraints are not yet supported.
Supported
Referential integrity
ElectricSQL maintains referential integrity of foreign key references. So you can use foreign key relationships in your data model and rely on referential integrity:
CREATE TABLE posts (
id UUID PRIMARY KEY
);
CREATE TABLE comments (
id UUID PRIMARY KEY
post_id UUID REFERENCES(posts.id) ON DELETE CASCADE
);
This works even when making writes locally in an offline database. See Introduction -> Conflict-free offline -> Preserving data integrity and the Rich-CRDT post on Compensations for more information.
Not-null constraints
ElectricSQL supports not-null constraints as long as the constraint is defined when creating the column.
I.e.: the not-null constraint must be defined in an additive migration. So the following is supported because creating the table with new columns is additive:
CREATE TABLE items (
-- implicit non null constraint
id UUID PRIMARY KEY
-- explicit non null constraint
foo TEXT NOT NULL
-- can be null
bar TEXT
)
Adding a column with a not-null constraint is supported because it's additive:
ALTER TABLE items
ADD COLUMN baz TEXT NOT NULL;
Constraining an existing column by adding a not-null constraint to it is not supported:
-- Not supported
ALTER TABLE items
ALTER COLUMN bar TEXT NOT NULL;
This is not supported because it may invalidate concurrent, in-flight operations. Specifically, writes that were accepted locally with null values would need to be rejected, which would violate the finality of local writes.
Unsupported
Unsupported constraints must be removed from a table before electrifying it.
Check constraints
ElectricSQL does not currently support check constraints.
Unique constraints
ElectricSQL does not currently support unique constraints.
We're working to support:
- unique constraints using Reservations
- single column and then multi-column check constraints using validation
See Rich-CRDTs for more information.