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.

Model.filter not generating correct SQL for join clause

See original GitHub issue

I have three models in a heritical order:

class CrawlProject(Model):
    name = CharField()

class CrawlGroup(Model):
    name = CharField()
    project = ForeignKeyField(CrawlProject)

class CrawlRule(Model):
    name = CharField()
    group = ForeignKeyField(CrawlGroup)

Given the following code:

CrawlRule.filter(group__project__id=47)

Then I got the following error:

 File "/home/tiger/repos/baelish/.venv/lib/python3.6/site-packages/MySQLdb/cursors.py", line 378, in _query
   db.query(q)
 File "/home/tiger/repos/baelish/.venv/lib/python3.6/site-packages/MySQLdb/connections.py", line 280, in query
   _mysql.connection.query(self, query)
eewee.OperationalError: (1054, "Unknown column 't3.project_id' in 'on clause'")

The generated SQL is like:

SELECT `t1`.`id` FROM `crawl_rule` AS `t1`
INNER JOIN `crawl_project` AS `t2`ON (`t3`.`project_id` = `t2`.`id`)
INNER JOIN `crawl_group` AS `t3` ON (`t1`.`group_id` = `t3`.`id`)
WHERE ((`t3`.`project_id` = 47)) LIMIT 20 OFFSET 0

It seems that t3 was referenced before defined.

actually, simply swapping the inner joins works:

SELECT `t1`.`id` FROM `crawl_rule` AS `t1`
INNER JOIN `crawl_group` AS `t3` ON (`t1`.`group_id` = `t3`.`id`)
INNER JOIN `crawl_project` AS `t2`ON (`t3`.`project_id` = `t2`.`id`)
WHERE ((`t3`.`project_id` = 47)) LIMIT 20 OFFSET 0

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
yifeikongcommented, Nov 29, 2019

Thanks! peewee has the lowest open-issue/star ratio in all the repositories I have seen on GitHub. ^_^

0reactions
coleifercommented, Nov 28, 2019

Fixed.

Read more comments on GitHub >

github_iconTop Results From Across the Web

SQL Joins Using WHERE or ON - Mode Analytics
Normally, filtering is processed in the WHERE clause once the two tables have already been joined. It's possible, though that you might want...
Read more >
SQL Filter criteria in join criteria or where clause which is more ...
Using the criteria at ON clause will not filter/skip the rows to select instead the join columns would be null based on the...
Read more >
MySQL 8.0 Reference Manual :: 13.2.13.2 JOIN Clause
In MySQL, JOIN , CROSS JOIN , and INNER JOIN are syntactic equivalents (they can replace each other). In standard SQL, they are...
Read more >
Query syntax | BigQuery - Google Cloud
The HAVING clause filters the results produced by GROUP BY or aggregation. GROUP BY or aggregation must be present in the query. If...
Read more >
Propel Query Reference
Filters on array columns translate to SQL as LIKE conditions. That means that the resulting query often requires an expensive table scan, and...
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