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.

QuerySet build sql error When multi ForeignKeyField ref to one model

See original GitHub issue

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

github_iconTop GitHub Comments

5reactions
zengsuncommented, Jan 19, 2020

It’s OK ! The problem has been solved.

0reactions
grigicommented, Jan 19, 2020

Released as v0.15.9

Read more comments on GitHub >

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

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