refresh_from_db silently ignores fields supplied as an iterator
I maintainer di solito rispondono entro 2 giorni
Valutazione
- Difficoltà
- 2/5
- Tempo stimato
- 1-3 ore
- Idoneità per principianti
- 35/100
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
- Nessun Dockerfile né file Docker Compose
- Ha un modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di tortoise/tortoise-orm
-
pydantic_model_creator returns a cached class that ignores model_config, validators and moduleForse già presa @moneebullah25 l’ha presa 6 giorni fa. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 84/100
tortoise/tortoise-orm#2310 ·
I maintainer di solito rispondono entro 2 giorni
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 65/100
tortoise/tortoise-orm#668 ·
I maintainer di solito rispondono entro 2 giorni
-
Renamed fields break dict(model) and default values_listForse già presa @IlyaKhalafi l’ha presa 1 giorno fa. Aperta
Difficoltà 3/5 1-2 giorni Idoneità per principianti 25/100
tortoise/tortoise-orm#2337 ·
I maintainer di solito rispondono entro 2 giorni
-
Reverse relation filters use attribute names for SQL columnsForse già presa @IlyaKhalafi l’ha presa 1 giorno fa. Aperta
Difficoltà 4/5 3-5 giorni Idoneità per principianti 25/100
tortoise/tortoise-orm#2336 ·
I maintainer di solito rispondono entro 2 giorni
-
Full refresh_from_db leaves source_field attributes staleForse già presa @IlyaKhalafi l’ha presa 1 giorno fa. Aperta
Difficoltà 3/5 1-2 giorni Idoneità per principianti 20/100
tortoise/tortoise-orm#2334 ·
I maintainer di solito rispondono entro 2 giorni
Tutte le issue di tortoise/tortoise-orm
Issue simili
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 88/100
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
aws-samples/sample-per-model-bedrock#261 ·
I maintainer di solito rispondono entro 1 giorno
-
[UI/Accessibility] Low contrast text visibility in country selection dropdown on settings pageAperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 65/100
pwncollege/dojo#1188 ·
I maintainer di solito rispondono entro 1 giorno
-
bug launcher packaging
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 88/100
tenstorrent/tt-studio#1426 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
python-attrs/cattrs#796 ·
I maintainer di solito rispondono entro 1 giorno