psandler wrote:
As a quick aside, we were unable to accomplish the original task in query analyzer with a single INSERT statement.
That's logical, as you need the value of the PK at insert time, but you only know that after insert time
. Classical example of a loop in the model
Otis wrote:
You're assigning an entity to itself, may I ask why?
It's a self-referencing table that allows for parent-child trees. If the parent's id is the same as the child's id, that indicates the top of the tree (the row has no parent).
We considered doing it a number of alternative ways:
- We could leave the column null if it has no parent.
- We could set the column to a dummy row in the table (like -100 or 0).
We prefer the self-relating method better because:
Alternative #1 relies on NULLs
Alternative #2 relies on hard-coded business logic ("if xxx = -100")
Your situation also relies on NULLs. You can't save a row which has an FK to itself and requires the same value for the FK field as the PK field if you don't use a nullable field for the fk IF you have an autonumber PK.
However, we are not hard-wired into this solution yet, so suggestions are welcome.
Well, pointing to self with a parent/child hierarchy is an infinite loop, and therefore not supported. (the recursive save sees that the entity to save is already in progress, so quits, and therefore the FK side doesn't get saved)
It's also semantically wrong. (sorry
). -> the 'root' does have a parent, namely itself. And that parent does have a parent as well (itself) till oblivion.
The root is the one with NO parent, i.e.: parent is undefined. So I'd suggest you go for option 1): the root's Parent is NULL.