tortoise / tortoise/tortoise-orm

There is an entry for table "table", but it cannot be referenced from this part of the query

Open
#1,638 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
5.6k
Forks
516
Avg merge
2d 21h
Merged PRs (30d)
9

Description

Describe the bug
I have two models. They are related via OneToOneField.

class User(Model):
    class Meta:
        table = "users"

    id = fields.UUIDField(primary_key=True)
    internal_id = fields.IntField(unique=True)
    info: fields.ReverseRelation["UserInfo"]

class UserInfo(Model):
    class Meta:
        table = "user_infos"

    id = fields.UUIDField(primary_key=True)
    rank = fields.IntField()
    user = fields.OneToOneField("app.User", related_name="info")

When I try to update UserInfo by user's internal_id I got an error:

await UserInfo.filter(user__internal_id=SOME_ID).update(rank=5)
tortoise.exceptions.OperationalError: invalid reference to FROM-clause entry for table "user_infos"
HINT:  There is an entry for table "user_infos", but it cannot be referenced from this part of the query.

SQL Query:

UPDATE "user_infos" SET "rank"=$1 FROM "user_infos" "user_infos_" LEFT OUTER JOIN "users" "user_infos__user" ON "user_infos__user"."id"="user_infos"."user_id" WHERE "user_infos__user"."internal_id"=893706004;

If I call just filter without update it works fine. It returns UserInfo object:

await UserInfo.filter(user__internal_id=internal_id)

Expected behavior
Update request executes successfully.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the update path used by UserInfo.filter(user__internal_id=...).update(...) and compare it with the working filter path, using the SQL shown as the failing case. Done means the update executes successfully for the OneToOne relation without the invalid FROM-clause reference.

Written by the indexing model from the issue text.

Assessment

Tech stack
postgresql, python
Domain
backend, databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.