Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

Reverse relation filters use attribute names for SQL columns

Aperta
#2,336 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 2 giorni

@IlyaKhalafi ci sta già lavorando.

Dal 15/10/2026.

  • #2338 di @IlyaKhalafi — aperta

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
25/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
python, sqlite
Ambito
databases

Direzione di ricerca

Start with the reproduction script in the issue and print query.sql() to see which names reach the SQL. The bug is that the reverse-relation filter uses model attribute names (id, parent_id) where the table needs source_field column names (child_pk, parent_fk). Pull request #2338 is already open against this issue, so review or coordinate on it before starting new work. Done means the query returns the parent on SQLite and the one-to-one primary-key case in #2323 also works.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Reverse relation filters use model attribute names where SQL needs database column names. This affects the compared primary key and the join key when source_field is set.

Reproduced on develop at ac38923 with Python 3.14 and SQLite.

from tortoise import Tortoise, fields, run_async
from tortoise.models import Model

class Parent(Model):
    name = fields.TextField()

class Child(Model):
    id = fields.UUIDField(primary_key=True, source_field="child_pk")
    parent = fields.ForeignKeyField(
        "models.Parent", related_name="children", source_field="parent_fk"
    )

async def run():
    await Tortoise.init(db_url="sqlite://:memory:", modules={"models": ["__main__"]})
    try:
        await Tortoise.generate_schemas()
        parent = await Parent.create(name="parent")
        child = await Child.create(parent=parent)
        query = Parent.filter(children__in=[child])
        print(query.sql())
        print(await query)
    finally:
        await Tortoise.close_connections()

run_async(run())

The query refers to child.id and child.parent_id, but the table has child_pk and parent_fk. It raises OperationalError instead of returning the parent.

The same primary-key mapping problem affects a reverse one-to-one relation whose primary key is the forward relation. It was reported in the review of #2323.

Related reports #1710 and #2029 were closed with workarounds. The failure still occurs on current develop. I also tested the current head of #2299, which addresses other one-to-one primary-key problems, and the reverse membership query still fails there.

Lingua principale
Python
Stelle
5.6k
Fork
523
Merge medio
2g 18h
PR unite (30g)
12

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di tortoise/tortoise-orm

Tutte le issue di tortoise/tortoise-orm

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.