Model.filter not generating correct SQL for join clause
See original GitHub issueI 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:
- Created 4 years ago
- Comments:6 (2 by maintainers)
Top 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 >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
Thanks! peewee has the lowest open-issue/star ratio in all the repositories I have seen on GitHub. ^_^
Fixed.