How to create integrity constraints?

Let’s suppose I have an Instance: Invoice with Total 100 usd.
And I want to also insert the items of the invoice:
item A: 33usd
item B: 33usd
item C: 34usd…

So the constraint is that the Sum of the itemized items must be equal to the Invoice total.

And if the constraint fails, then the transaction should be cancelled.

Can you point me in the right direction.
Thank you.

In TypeDB 2.x this is difficult. You might think this is a job for rules, but rules don’t have access to aggregates (like sums). The only option you have is a client-side verification of the constraint: i.e.

  1. first, insert the items.
  2. then, match items and sum their total; also retrieve specified amount on invoice.
  3. finally, in your client code, compare sum total with specified amount of invoice. Only commit the transaction if this verification passes.

In TypeDB 3.x (in alpha at the moment, with the first stable release expected by the end of this year) you can use functions to do this comparison completely on the server-side. We are further planning on adding automation of certain function calls (“triggered” by data inserts and data changes ) which will allow such constraints to be expressed as part of your schema.

Btw, you’ll get a hold of us faster on our Discord server!

1 Like