refresh_from_db silently ignores fields supplied as an iterator
メンテナーはふだん 2 日以内に返信
評価
- 難易度
- 2/5
- 見積もり時間
- 1〜3時間
- 初心者へのやさしさ
- 35/100
調査の方向性
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.
索引モデルが issue の本文から書いたものです。
説明
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.
- 主要言語
- Python
- スター
- 5.6k
- フォーク
- 523
- 平均マージ
- 2日 18時間
- マージ済み PR(30日)
- 12
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
tortoise/tortoise-orm のほかの issue
-
pydantic_model_creator returns a cached class that ignores model_config, validators and module対応中かも @moneebullah25 が 6 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 84/100
tortoise/tortoise-orm#2310 ·
メンテナーはふだん 2 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 65/100
tortoise/tortoise-orm#668 ·
メンテナーはふだん 2 日以内に返信
-
Renamed fields break dict(model) and default values_list対応中かも @IlyaKhalafi が 1 日前に担当しました。 オープン
難易度 3/5 1〜2日 初心者へのやさしさ 25/100
tortoise/tortoise-orm#2337 ·
メンテナーはふだん 2 日以内に返信
-
Reverse relation filters use attribute names for SQL columns対応中かも @IlyaKhalafi が 1 日前に担当しました。 オープン
難易度 4/5 3〜5日 初心者へのやさしさ 25/100
tortoise/tortoise-orm#2336 ·
メンテナーはふだん 2 日以内に返信
-
Full refresh_from_db leaves source_field attributes stale対応中かも @IlyaKhalafi が 1 日前に担当しました。 オープン
難易度 3/5 1〜2日 初心者へのやさしさ 20/100
tortoise/tortoise-orm#2334 ·
メンテナーはふだん 2 日以内に返信
tortoise/tortoise-orm の issue をすべて見る
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 65/100
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
UKGovernmentBEIS/control-arena#917 ·
メンテナーはふだん 1 日以内に返信
-
難易度 1/5 1時間未満 初心者へのやさしさ 88/100
-
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
debpalash/VoiceStudio#2937 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
aws-samples/sample-per-model-bedrock#261 ·
メンテナーはふだん 1 日以内に返信