F
F
FFSinit2014-07-06 10:02:53
MySQL
FFSinit, 2014-07-06 10:02:53

SQLAlchemy. How to select from a tree only those elements that have links in another table?

There are two tables, in one there is a tree of categories (table1) and subcategories, in the second there is useful data (table2)
The structure of the tables is something like this:
table1

`idn` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) DEFAULT NULL,       # название
  `parent` int(11) DEFAULT NULL,           # id родителя

table 2
`idn` int(11) NOT NULL AUTO_INCREMENT, 
  `idcat` int(11) DEFAULT NULL,                   # id категории

These tables must be used as is, they cannot be changed.
I need to select from table1 all subcategories of a certain category (now there are two levels of nesting, it is unlikely to be more) and related records from table2. In some categories, there are no links to table2.
A head-on solution, to go through all this in a loop, there will be a lot of requests to the database, I would not want to do this.
I would like to select all this for a couple of requests at most, and get a tree structure at the output.
How to do it in SQLAlchemy? I used to deal a little with Django ORM, I don’t understand SQLAlchemy at all.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sim3x, 2014-07-06
@FFSinit

You can get a tree for no reason, a list - you can.
For django this would be a query with select_related()
it is not clear here - categories that do not have products, also display or not?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question