Sergey M
Sergey M

Reputation: 521

Sqlite circular dependency trigger

The question is about Sqlite DBMS.

I have two tables:

  1. describes objects
  2. describes dependencies of objects from table 1 { id, id_parent }

How to write an INSERT and UPDATE triggers for table 2 to check for circular dependencies?

Example of objects dependencies:

A -> B -> C --- ok

A -> B -> C -> A --- raise condition

I understand how to check the immediate dependency A -> B, but have no idea how to check for any level.

Upvotes: 0

Views: 930

Answers (1)

Marcelo Cantos
Marcelo Cantos

Reputation: 185922

You're pushing the proverbial uphill.

One fairly simple solution, which may or may not be feasible in your circumstances, is to have a constraint on the second table: id > id_parent. This will make it impossible to create cyclic graphs:

CREATE TABLE deps ( id NOT NULL, id_parent NOT NULL, CHECK ( id > id_parent ) )

Upvotes: 1

Related Questions