SQL Server cycle detection doesn't detect multiple paths correctly #8969
Labels
bug/2-confirmed
Bug has been reproduced and confirmed.
kind/bug
A reported bug.
tech/engines/datamodel
Issue about parsing/validation/rendering of the Prisma schema
topic: sql server
Microsoft SQL Server
Milestone
SQL Server and multiple cascade paths
Our original implementation of cycle detection has a problem: it only detects cycles, never multiple cascading paths correctly. By accident, all tests were green, until our introspection CI catched an issue that should validate and we can safely migrate to the database.
What are multiple cascading paths
A simple example from our current test setup demonstrates a data model that should not pass the validation:
Originally, our setup validated this by detecting a cycle:
A.b -> B.cs -> C.a
and so on. This is wrong. The actual validation error in the data model goes in two paths:C -> A -> B
andC -> B
.By accident, our cycle detection algorithm validated this, but by looking into a more complex example, we see what is wrong with the current algorithm:
Our current validation finds a cycle in it, as the algorithm is written:
Although when turning off the validation, SQL Server accepts the migration!
The first step is to stop jumping over back-relations and only look for
@relation
arguments with fields, references and (maybe implicitly) refererential actions defined. From this data model we can find several paths:order_items -> orders -> stores
order_items -> products
From
order_items
there are no cascading paths.stocks -> products
stocks -> stores
Again, no cascading paths.
From this point on we're done and we don't need to be looking into other paths.
The text was updated successfully, but these errors were encountered: