Joining with no ON condition and no FK.
See original GitHub issue(Postgres 10 and peewee version 3.7.0)
To make this simple to test, I’ve assembled some ready to use SQL:
create table table1(
id serial primary key
);
create table table2(
id serial primary key
);
create table table3(
id serial primary key,
table1_id int,
table2_id int
);
insert into table1 (id) values (1), (2), (3);
insert into table2 (id) values (1), (2);
insert into table3 (id, table1_id, table2_id) values (1, 1, 1), (2, 1, 2), (3, 2, 1);
I want to execute the following query, using Peewee:
SELECT t1.id, t2.id, t3.id
FROM table1 t1
CROSS JOIN table2 t2
LEFT JOIN table3 t3
on t3.table1_id = t1.id and t3.table2_id = t2.id
where t3.id is null;
Note the CROSS JOIN with no “on” condition.
I’ve tried the following:
results = (table1.select(table1.id.alias("table1_id"), table2.id.alias("table2_id"), table3.id.alias("table3_id"))
.join(table2, JOIN.CROSS)
.join(table3, JOIN.LEFT_OUTER, on=((table3.table1_id == table1.id) & (table3.table2_id == table2.id)))
.where(table3.id.is_null()))
This is returning ValueError: Unable to find foreign key between <Model: Table1> and <Model: Table2>. Please specify an explicit join condition.
I’ve also tried replacing the CROSS JOIN with some raw sql (helps in many other edge cases with peewee):
.join(SQL("CROSS JOIN table2"))
This is in turn returning UnboundLocalError: local variable 'constructor' referenced before assignment
Is there any way to get this query to work?
Issue Analytics
- State:
- Created 5 years ago
- Comments:9 (6 by maintainers)
Top Results From Across the Web
How to use MySQL JOIN without ON condition? - Tutorialspoint
We can use 'cross join' without on condition. Cross join gives the result in cartesian product form. For instance, if in one table...
Read more >Join without foreign key for condition - Stack Overflow
To me it seems there IS some hidden thing that is actually linking the rows, otherwise the query would make no sense.
Read more >Can we join two tables without primary and foreign key ...
Yes we can. No Clause says that for joining of two or more tables there must be a foreign key or primary key...
Read more >JOIN Tables Without Foreign Key In SQL Server - C# Corner
In this blog, we will understand how to join two or more SQL tables without using a foreign key. We will also get...
Read more >Can you Join two Tables Without a Common Column?
Yes, you can! The longer answer is yes, there are a few ways to combine two tables without a common column, including CROSS...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Working up a fix.
No worries. You’re an absolute star for getting on this so quickly!