Referential Integrity and Constraints
Introduction
Starcounter does not have complete support for referential integrity or constraints.
Referential integrity can instead be achieved using commit hooks which allow the developer to ensure that the correct corresponding item is deleted or added when removing or committing an item to the database.
Referential Integrity with Commit Hooks
Commit hooks should be implemented in a separate class and then registered when the application is started. Here's an example of that:
Let's say you have two DB classes: parent Order
and child OrderItem
:
Here the commit hooks are declared in a separate class in a Register
method:
In this code, we register the hooks by using var hooks = new Hooks();
and hooks.Register();
.
Two things that are done here:
When making an
INSERT
orUPDATE
, the commit hooks will check if the reference to the database object is set properly. If not, then it throws an exception and prevents the item from being updated or saved to the database.Before deleting a parent entity it deletes the children of that entity.
OnDelete
As an alternative to the BeforeDelete
commit hook, you can use the Starcounter method OnDelete
.
OnDelete
works similar to the OnData
and HasChanged
callback methods that are explained on the callback methods page. It executes some code every time an instance of that class is deleted.
This is how it looks in code:
If we create a new instance of the class Foo
and then delete it by calling fooInstance.Delete()
we would run CalledWhenInstanceIsDeleted()
.
Constraints
Database constraints define certain requirements that a database has to comply with.
Starcounter does not have database constraints as a part of its schema definition. As a consequence of this, you cannot define constraints in Starcounter the same way you would do with most SQL databases.
One of the most common constraints is the unique constraint which states that values in specified columns must be unique for every row in the table. This constraint can be set in Starcounter, even though it's not a part of the schema definition, by using indexes.
Last updated