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

refresh_from_db silently ignores fields supplied as an iterator

Aperta
#2,341 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.

  • #2342 di @IlyaKhalafi — aperta

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
35/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
python, sqlite
Ambito
database

Direzione di ricerca

The bug is in Model.refresh_from_db(fields=...), where the iterable is consumed by .only(*fields) before the refreshed values are copied back, so an iterator behaves differently from a list. Start by reading refresh_from_db and the .only() call it makes, then run the reproduction script in the issue against SQLite to confirm before 1 is printed. Done means an iterator of field names refreshes the same fields as the equivalent list, and an empty iterator behaves like an empty list. Pull request #2342 already targets this, so check it before starting.

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

Descrizione

Model.refresh_from_db(fields=...) accepts Iterable[str], but a generator is consumed by .only(*fields) before the refreshed values are copied back. The query succeeds while the model instance keeps its stale values.

Reproduced on develop at ef637d4 with SQLite and Python 3.14.

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

class Counter(Model):
    name = fields.CharField(max_length=30)
    count = fields.IntField(default=1)

async def main():
    await Tortoise.init(db_url="sqlite://:memory:", modules={"models": ["__main__"]})
    try:
        await Tortoise.generate_schemas()
        obj = await Counter.create(name="before", count=1)
        await Counter.filter(pk=obj.pk).update(name="after", count=2)
        await obj.refresh_from_db(fields=iter(["name"]))
        print(obj.name, obj.count)
    finally:
        await Tortoise.close_connections()

run_async(main())

This prints before 1. Passing ["name"] prints the expected after 1.

An empty field iterator also raises ValueError from .only(), while an empty list refreshes all fields. Field iterators should behave like the equivalent lists and preserve selective refresh.

This differs from #2335, which fixes model attribute names when columns are renamed.

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.