question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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:closed
  • Created 5 years ago
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
coleifercommented, Sep 8, 2018

Working up a fix.

0reactions
joaodlfcommented, Apr 24, 2019

No worries. You’re an absolute star for getting on this so quickly!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found