Best method to verify multi-level relational dependencies

前端 未结 2 1005
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-06 05:31

Suppose you have entities A, B, C and D.

  • D relates to C
  • C relates to B
  • B relates to A

Furthermore, user is only allowed to <

相关标签:
2条回答
  • 2021-01-06 06:05

    I'm not sure I understand what you want to achieve but can't you use option A, to verify if user is allowed to operate on D with only one access to the database?:

    SELECT D.*
      FROM D
        JOIN C 
          ON C.id = D.cid
        JOIN B
          ON B.id = C.bid
        JOIN A.id = B.aid
    WHERE A.ownedBy = @userID
      AND D.id = @idToBechecked  
    
    0 讨论(0)
  • 2021-01-06 06:06

    The obvious solution would be this: Given D, find C, then find B and eventually find A. If the chain breaks somewhere, access to D would be rejected. Unfortunately, this requires - if trivially implemented - 4 database accesses instead of just the one for A.

    I suppose that might be possible. It depends in part on what "relates to" means, but assuming a relatively straightforward schema, I'd expect you to be able to join all four tables in a single SQL statement. If part of the chain is missing, the query would return no rows.

    Or am I missing something?

    0 讨论(0)
提交回复
热议问题