Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8208601
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T09:25:45+00:00 2026-06-07T09:25:45+00:00

This is the class I am working with: https://gist.github.com/2174233 I have two tables: one

  • 0

This is the class I am working with: https://gist.github.com/2174233

I have two tables: one table contains users and the other – closures.

When I use get_children() method (let’s say from the root or another parent), it show only the first level of the hierarchy. What values should I store in lvl column?

This is the definition of closure table, that I have:

 CREATE TABLE closures (
     id         INT(11) NOT NULL AUTO_INCREMENT,
     ancestor   INT(11) NOT NULL,
     descendant INT(11) NOT NULL,
     lvl        INT(11) NOT NULL,

     PRIMARY KEY (id)
 )

Here’s an example of data that i store in the closure table:

INSERT INTO `closures` (`id`, `ancestor`, `descendant`, `lvl`) 
VALUES (1, 1, 20, 0),
       (4, 20, 26, 0),
       (5, 26, 25, 0);

The last is the lvl column, but I don’t know, what values goes there. Can you give me an advice of how to use it?

The structure that i have to store has 3 levels : root -> 20 node -> 26 node, but it gives me only the first level children that is the 20 node.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T09:25:49+00:00Added an answer on June 7, 2026 at 9:25 am

    I would recommend for you to pick up the SQL Antipatterns book. The second chapter feature closure tables as one of recommended ways for implementing category trees.

    That said. Looks like your closure table is a bit strange. There is no point in id column there. Instead you should have a composite primary key, made from unique pairs of ancestor and descendant value.

    And you have not inserted the nodes themselves .. only the connection between two different nodes. Maybe reading “Rendering Trees with Closure Tables” could shine some light on the subject.

    At a guess the INSERT statement should look like this (at least that my conclusion):

    INSERT INTO closures(ancestor, descendant, lvl) 
    VALUES (1,  1,  null),
           (20, 20, null),
           (26, 26, null),
           (28, 28, null),
           (1,  20, 1),
           (20, 26, 2),
           (26, 25, 3);
    

    What you have to understand is that closure tables are not storing a tree. Instead, the data structure you are working with, is directional graph. Something like this:

    enter image description here

    As you can see, this graph has three root nodes: 3, 5 and 7. Also, it is very important to note, that node 10 is a different levels of depth, depending on from which root node you begin.

    It would be defined with two closures: [3,10,1] and [11,10,2]. Meaning, that the connection from 11th node would put it a level two, while starting from 3rd node, it’s a first level item.

    Thing is, when you are using closure tables each category can have multiple parent categories each at different level of depth.


    Addition (by @ypercube):

    My understanding of the “level” or “depth” column is that it stores the “distance” (steps needed to go) from ancestor to descedant. It’s not an absolute level of a node and thus closure tables can be used to store more complex than trees graphs. You may even have multiple paths from an ancestor to a descendant, each one (path) with different steps.

    Additionally, the Nulls should be 0 and a few more rows are needed.

    So, the data would be:

    INSERT INTO closures(ancestor, descendant, lvl) 
    VALUES ( 1,  1,  0), (20, 20, 0), (26, 26, 0), (25, 25, 0),
                         ( 1, 20, 1), (20, 26, 1), (26, 25, 1),
                                      ( 1, 26, 2), (20, 25, 2),
                                                   ( 1, 25, 3) ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I installed this gem: https://github.com/crowdint/rails3-jquery-autocomplete and applied it successfully: class TagsController < ApplicationController autocomplete
I'm working with Osmdroid, particularly with this class: http://code.google.com/p/osmdroid/source/browse/trunk/osmdroid-android/src/org/osmdroid/views/overlay/ItemizedOverlayWithFocus.java It shows a ballon with
I have a working custom UserNamePasswordValidator that calls into my Oracle DB. This class
I have this program that I am working on for class, I think the
I'm using the ajaxify-html5.js script found here: https://github.com/browserstate/ajaxify The script is working great, except
I'm working to the following little project: https://github.com/AndreaCrotti/project-organizer Which in short aims to manage
I'm having a problem similar to [this question][1] https://stackoverflow.com/questions/1449072/debugging-problem-in-x-code-iphone-development I have created an application,
I'm using this great plugin: https://github.com/maranomynet/linkify/blob/master/1.0/jquery.linkify-1.0.js to link text manipulating the dom. The problem
I'm using abraham's twitter oauth class (https://github.com/abraham/twitteroauth) to connect and update twitter. I had
I am working with a Django model that looks like this: class Subscription(models.Model): user

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.