The adjacency list pattern is a common
relational pattern whereby a table
contains a foreign key reference to
itself. This is the most common and
simple way to represent hierarchical
data in flat tables. The other way is
the “nested sets” model, sometimes
called “modified preorder”. Despite
what many online articles say about
modified preorder, the adjacency list
model is probably the most appropriate
pattern for the large majority of
hierarchical storage needs, for
reasons of concurrency, reduced
complexity, and that modified preorder
has little advantage over an
application which can fully load
subtrees into the application space.
SQLAlchemy commonly refers to an
adjacency list relationship as a
self-referential mapper. In this
example, we’ll work with a single
table called treenodes to represent a
tree structure: