New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[BUG] Frozen fields not supported (Pydantic 2) - exception thrown on .save()
#863
Comments
Frozen models don't work either, but I guess this is expected because of the It'd be really nice if it was possible to mark a model as For completeness, here's a reproduction case for a frozen=True model in Pydantic 2: from pydantic import Field, ConfigDict
from motor.motor_asyncio import AsyncIOMotorClient
from beanie import Document, init_beanie
class TestFrozen(Document):
model_config = ConfigDict(frozen=True)
some_field: str
class Settings:
name = "test_frozen"
async def test_frozen():
await init_beanie(database=client[DB_NAME], document_models=[TestFrozen])
test_frozen = TestFrozen(some_field="test")
await test_frozen.save()
# also fails with test_frozen.insert()
await test_frozen() Result:
|
.save()
.save()
.save()
Possible workaround for frozen fields:
I'm still testing this to confirm there's no edge cases, but this might be a usable workaround for me. I'd still raise the |
Unfortunately I don't think the state_management=True work around works, at least not if links are used. Because I also get the frozen fields exception when I do something like: from beanie import Document, Link, WriteRules
from pydantic import Field
class Door(Document):
height: int = Field(default=2, frozen=True)
width: int = 1
class House(Document):
name: str
door: Link[Door]
door = Door(height=10, width=20)
house = House(name="test", door=door)
await house.insert(link_rule=WriteRules.WRITE) Even though nothing has changed on |
Hi! Good catch. I'll add a test suite for this and will fix on the next bug-fixing session. |
Describe the bug
Beanie does not appear to support frozen fields.
Testing with Beanie 1.25.0 on Pydantic 2.6.1.
Trying to
.save()
a model with a frozen field throws a validation error. Usinginsert()
does work, but then it will fail if you ever call.save()
on it.Exception thrown:
Exception thrown from
beanie/odm/utiils/parsing.py
:To Reproduce
Expected behavior
Frozen fields are handled, and do not break saving. I guess it will be necessary to modify
merge_models()
to check on a per field basis if a field is frozen, and handle it differently.Thanks in advance.
The text was updated successfully, but these errors were encountered: