Open z0z0r4 opened 6 months ago
if primary_field is None:
if "id" in odm_fields:
if odm_fields["id"].key_name in ("id", None):
raise TypeError(
"can't automatically generate a primary field since an 'id' "
"field already exists"
)
But another error
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "D:\projects\mcim-backend\.venv\Lib\site-packages\odmantic\model.py", line 543, in __init__
super().__init__(**data)
File "D:\projects\mcim-backend\.venv\Lib\site-packages\pydantic\main.py", line 176, in __init__
self.__pydantic_validator__.validate_python(data, self_instance=self)
pydantic_core._pydantic_core.ValidationError: 2 validation errors for Project
id.is-instance[ObjectId]
Input should be an instance of ObjectId [type=is_instance_of, input_value='A', input_type=str]
For further information visit https://errors.pydantic.dev/2.7/v/is_instance_of
id.chain[union[str,bytes],function-plain[validate_from_string_or_bytes()]]
Value error, Invalid ObjectId [type=value_error, input_value='A', input_type=str]
For further information visit https://errors.pydantic.dev/2.7/v/value_error
It seems that no way to solve it?
class Project(Model): proid: str = Field(unique=True, key_name="project_id")
Every document needs to have a UUID, what you are doing is using the name 'id' to store something else, but this is a reserved name for the primary field.
You will have to define another field for the primary field:
class Project(Model):
uuid: ObjectId = Field(primary_field=True, default_factory=ObjectId)
id: int = Field(key_name='project_id')
Bug
key_name
doesn't work whenid
Current Behavior
TypeError: can't automatically generate a primary field since an 'id' field already exists
Expected behavior
work as
proid
successfullyEnvironment
python -c "import pydantic.utils; print(pydantic.utils.version_info())
):Additional context
check
key_word
plz