SQLite Forum

cycle detection in CHECK constraint with recursive CTE
Login
Here is stackoverflow question that seems to refer to your problem https://stackoverflow.com/questions/39555616/detect-cycles-using-sql