S
S
Sergey Eremin2014-12-08 04:09:11
Django
Sergey Eremin, 2014-12-08 04:09:11

Doesn't SQLite3 support double (nested) INNER JOIN queries?

Some time ago it turned out that in my case it is more efficient to write RAW queries in Django than to use ORM: How to make a SQL query with INNER JOIN in Django through ORM? ...
Now faced with the fact that a relatively simple question does not want to be executed at all. The request is something like this:

SELECT TAB1.id, TAB2.id, TAB2.something, TABLINK1TO2.quantity
FROM TAB2
    INNER JOIN (TAB1
        INNER JOIN TABLINK1TO2
        ON TAB1.id = TABLINK1TO2.link4tab1_id)
    ON TAB2.id = TABLINK1TO2.link4tab2_id
WHERE TAB1.id=1
GROUP BY  TAB1.id, TAB2.id, TAB2.something, TABLINK1TO2.quantity;

raw request returns:
<RawQuerySet: 'the query actually sent here'>

and accessing its elements causes errors and swearing at the lack of fields in the database:
no such column: TAB1.id

Actually, the exclusion of fields from the request leads to swearing at the next field ... when there are none left, then swearing at the fields inside the INNER JOIN ... In general, it does not work.
At the same time, literally simplifying the question by just one INNER JOIN:
SELECT TAB2.id, TAB2.something, TABLINK1TO2.quantity
FROM TAB2
    INNER JOIN TABLINK1TO2
    ON TAB2.id = TABLINK1TO2.link4tab2_id
WHERE TABLINK1TO2.link4tab1_id=1
GROUP BY  TAB2.id, TAB2.something, TABLINK1TO2.quantity;

And everything starts to go right.
Unfortunately, I need the first request, because in TAB1 there are more fields to be obtained (for simplicity and readability they are omitted). And it seems that the error is on the side of SQLite3. Could this be? Or just crooked hands?
It would be great if someone taught me how to build such queries through ORM, because. there is little data in these tables and speed is not critical. Unfortunately, there are no cases in the manuals for extracting data from three tables at once (two main tables and one ManyToManyField, but which has additional attributes.
PS Unfortunately, it is not possible to check the execution of a query through the SQLite3 console. The SQLite3 console does not accept the work of Ctrl-Ins + Shift-Ins (Ctrl-X + Ctrl-V), and it is unrealistic to type such a tricky SQL query without errors. The fact is that in a real project, tables and fields are named with long and complex names, and even DJANGO himself added a prefix from the project name to the table names ... Only in the FROM ... INNER JOIN (... INNER JOIN ... ON ....) ON ... something under 300 characters is obtained. So it's better not to try! :(
PPS Advice to change to a normal DBMS, like postgres. not suitable due to the fact that you have to code on one machine, then on another. Transferring the SQLite3 database file through some dropbox thread is as easy as shelling pears and it is equally picked up from both Windows and Ubuntu...

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
Oscar Django, 2014-12-08
@Sergei_Erjemin

A query like this works:

SELECT t1.id, t2.id, t2.something, t3.q
FROM t2 INNER JOIN t3 ON t2.id = t3.l2_id
        INNER JOIN t1 ON t1.id = t3.l1_id
WHERE t1.id=1
GROUP BY  t1.id, t2.id, t2.something, t3.q;

Means with double join'ami works.

V
Vladislav, 2014-12-08
@RGV

The error occurs because there is no id field in TAB1 (according to the error output no such column: TAB1.id). In a working query with one INNER JOIN, this field (and the table itself) does not appear at all.
If you are too lazy to drive in a request manually, then you can run the script from a file. On kraynyak, there is always a redirection of input / output streams.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question