'SQLAlchemy ORM Recursive query

This has been my starting point: https://docs.sqlalchemy.org/en/14/orm/query.html#sqlalchemy.orm.Query.cte

I am not an experienced programmer.

Background: I have written a recursive function in Python for traversing a parent-child structure in a MSSQL database. The depth of the structure can vary. I would like to transform my recursive python function into a recursive SQLAlchemy ORM query instead. The reason is that the server latency is a bit high, which leads to exceedingly long processing time. My hypothesis is that a recursive query will solve this issue.

First question: Is my hypothesis correct? Will a recursive query avoid the server latency with each call? If no, I guess this is where it ends. If yes, onwards:

I have two tables to consider: BOMVersion and BOM. A "BOM" in this case is a Parent (or sub-parent, etc.). BOMVersion contains all BOMs. Columns of interest: ItemID and BOMID. BOM contains BOM contents, i.e. children, of each BOM. Columns of interest: ItemID, BOMID, BOMQty. BOMQty is the quantity required of a child item in a BOM.

The program input is an ItemID. The program queries BOMVersion for the ItemID. If it's in, the BOMID is selected. Next step is to find the children of BOMID in BOM, and if a child is a BOM, rinse and repeat.

The problem I'm facing is that for all examples I can find parents and children are in the same table, including the parent link. In my scenario, the parent and parent link is not in the same table as the children, and I just can't make it work.

My code is basically the same as in the example provided at the top. Am I in the right area, conceptually? I thought about creating another CTE or similar (I'm not sure I understand what I'm talking about anymore) for the parents and children.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source