It's not really about the fact that children have higher pks. In this particular case the pages are already ordered by tree_id an lft. This code works based on the fact that pages are already ordered this way. If we change <code>if parent_id and parent_id in pages:</code> you can see that all the tests will break.