from sqlmodel import SQLModel
class Foo(SQLModel):
bar: str
class Config:
"""
Forbid mutation in order to freeze the inheriting classes
"""
allow_mutation = False
foo = Foo(bar='bar')
生产
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/tmp/ipykernel_3092/4259691966.py in <module>
----> 1 foo = Foo(bar='bar')
/usr/local/lib/python3.8/site-packages/sqlmodel/main.py in __init__(__pydantic_self__, **data)
512 object.__setattr__(__pydantic_self__, "__fields_set__", fields_set)
513 for key, value in values.items():
--> 514 setattr(__pydantic_self__, key, value)
515 non_pydantic_keys = data.keys() - values.keys()
516 for key in non_pydantic_keys:
/usr/local/lib/python3.8/site-packages/sqlmodel/main.py in __setattr__(self, name, value)
530 # non relationship values
531 if name not in self.__sqlmodel_relationships__:
--> 532 super().__setattr__(name, value)
533
534 @classmethod
/usr/local/lib/python3.8/site-packages/pydantic/main.cpython-38-x86_64-linux-gnu.so in pydantic.main.BaseModel.__setattr__()
TypeError: "Foo" is immutable and does not support item assignment
问题:可以禁止SQLModel
类的变异吗?
上下文:我有一个代码库,其中所有数据模型类都构建在冻结的Pydantic类上,我想从BaseModel
迁移到SQLModel
,以便存储继承的表类。
当然allow_mutation=True
的工作原理就像一个魅力。
1条答案
按热度按时间mqkwyuun1#
这可以说是
SQLModel.__init__
当前(0.0.8)实现中的一个bug。它调用它自己的
__setattr__
,__setattr__
又调用BaseModel.__setattr__
。由于禁止突变,所以你会得到错误。这已经在这里提到过了。我还没有看到修复它的PR,所以可能需要一段时间。同时,以下是可能的解决方法:
输出量: