QuerySet build sql error When multi ForeignKeyField ref to one model
See original GitHub issueDescribe the bug
class User(Model):
username = CharField(max_length=20, unique=True, description='用户名')
nickname = CharField(description='昵称', max_length=20)
email = CharField(description='EMail', max_length=50, null=True)
mobile_tel = CharField(description='手机', max_length=15, null=True)
class OperationLog(Model):
added_quantity = IntField(description='新增数量')
changed_quantity = IntField(description='更新数量')
operated_at = DateTimeField(auto_now_add=True, description='更新时间')
operated_by: ForeignKeyRelation[User] = ForeignKeyField('models.User',
description='更新人')
class History(Model):
num = CharField(max_length=14, description='设备编码')
log: ForeignKeyRelation[OperationLog] = ForeignKeyField(
'models.OperationLog',
related_name='created_histories',
description='操作记录')
changed_log: ForeignKeyRelation[OperationLog] = ForeignKeyField(
'models.OperationLog',
related_name='changed_histories',
description='变更操作')
qset = History.all().values('id', 'num', 'log__operated_at', 'log__operated_by__nickname', 'changed_log__operated_at', 'changed_log__operated_by__nickname')
query = await qset
qset.query.get_sql()
SELECT “history”.“id” “id”,“history”.“num” “num”,“operationlog”.“operated_at” “log__operated_at”,“user”.“nickname” “log__operated_by__nickname”,“operationlog”.“operated_at” “changed_log__operated_at”,“user”.“nickname” “changed_log__operated_by__nickname” FROM “history” LEFT OUTER JOIN “operationlog” ON “operationlog”.“id”=“history”.“log_id” LEFT OUTER JOIN “user” ON “user”.“id”=“operationlog”.“operated_by_id”
The generated SQL statement is obviously wrong !
Correct SQL statement:
SELECT
"a"."id" "id",
"a"."num" "num",
"b"."operated_at" "log__operated_at",
"c"."nickname" "log__operated_by__nickname",
"d"."operated_at" "changed_log__operated_at",
"e"."nickname" "changed_log__operated_by__nickname"
FROM "history" as a
LEFT OUTER JOIN "operationlog" as b ON b."id"=a."log_id"
LEFT OUTER JOIN "user" as c ON "c"."id"="b"."operated_by_id"
LEFT OUTER JOIN "operationlog" as d ON d."id"=a."changed_log_id"
LEFT OUTER JOIN "user" as e ON "e"."id"="d"."operated_by_id";
Issue Analytics
- State:
- Created 4 years ago
- Comments:7 (5 by maintainers)
Top Results From Across the Web
SQL Syntax error, while formulating Django queryset using extra
In a Django app named links, at one point I run the following code on a queryset, and getting the error: column user_id...
Read more >QuerySet API reference | Django documentation
Multiple parameters are joined via AND in the underlying SQL statement. ... An expression may be a simple value, a reference to a...
Read more >Making queries — Django 4.1.4 documentation
To create an object, instantiate it using keyword arguments to the model class ... In SQL terms, a QuerySet equates to a SELECT...
Read more >SQL FOREIGN KEY - W3Schools
A FOREIGN KEY is a field (or collection of fields) in one table that refers ... a FOREIGN KEY constraint on multiple columns,...
Read more >Modeling Polymorphism in Django With Python
Should you have multiple tables? How to reference instances of a polymorphic model: To utilize database and Django ORM features, you need to...
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
It’s OK ! The problem has been solved.
Released as v0.15.9