Answer the question
In order to leave comments, you need to log in
How to implement recursive reading of all descendants of an object in Gino()?
The base is designed so that each table element can have one or more child elements of the same table. Please help with the implementation of a database query that would return children, children of children, and so on - that is, all descendants of any element. The option to get the id of the child elements of this from the database, and then send requests to the database again to get the children of these children, etc. I don’t really like it because of the resource intensity. asyncpg, which gino works with, is faster than executing python code, so it makes more sense to build a recursive database query.
I know that this task is easily solved in sqlalchemy. An example of its solution is given for example here, however I need the request in gino. Gino, as far as I understand, does not support the use of the relationship method, instead, such requests are implemented somehow using loaders. the official manual has an example of using the load method to retrieve all immediate children of a given element here . But I don't know how to use it in solving my problem of recursively finding all children of a given parent.
In fact, the ideal solution would not even be the solution given in the link , but a solution using a slightly different data structure, if possible.
Namely, the following
class Nodes(db.Model):
__tablename__ = 'nodes'
id = db.Column(db.Integer, primary_key=True)
info = db.Column(db.Text)
class Edges(db.Model):
__tablename__ = 'edges'
id = db.Column(db.Integer, primary_key=True)
source = db.Column(db.Integer, db.ForeignKey('nodes.id'))
target = db.Column(db.Integer, db.ForeignKey('nodes.id'))
_edge_idx = db.Index('edge_idx', 'source', 'target', unique=True)
Answer the question
In order to leave comments, you need to log in
To search for parent-children, there is Self Referencing , though experimental.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question