aiondemand / aiondemand/AIOD-rest-api

Migration to Pydantic V2

未关闭
#573 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
wontfix
主要语言
Python
星标
36
派生
77
PR 合并指标
30 天内没有已合并 PR

描述

I experimented with migrating the codebase to Pydantic V2 ([here](https://github.com/aiondemand/AIOD-rest-api/tree/pydantic-v2)), but am running into several issues. Running tests on the last commit of that branch results in an error message:

```
...
File "/Users/pietergijsbers/repositories/aiod/src/tests/testutils/default_instances.py", line 16, in
from database.model.agent.contact import Contact
File "/Users/pietergijsbers/repositories/aiod/src/database/model/agent/contact.py", line 37, in
class Contact(ContactBase, AIoDConcept, table=True): # type: ignore [call-arg]
File "/Users/pietergijsbers/repositories/aiod/venv/lib/python3.11/site-packages/sqlmodel/main.py", line 559, in __new__
col = get_column_from_field(v)
^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/pietergijsbers/repositories/aiod/venv/lib/python3.11/site-packages/sqlmodel/main.py", line 708, in get_column_from_field
sa_type = get_sqlalchemy_type(field)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/pietergijsbers/repositories/aiod/venv/lib/python3.11/site-packages/sqlmodel/main.py", line 697, in get_sqlalchemy_type
raise ValueError(f"{type_} has no matching SQLAlchemy type")
ValueError: has no matching SQLAlchemy type
```
Now, the only `AIoDEntryORM` reference the model directly has, should be that defined in the `AIoDConcept`. Something that is similar to this issue: https://github.com/fastapi/sqlmodel/discussions/908
And it does indeed seem "resolved" by moving the relationship down to the `Contact` level, resulting in the next error: `sqlalchemy.exc.ArgumentError: Mapper Mapper[Contact(contact)] could not assemble any primary key columns for mapped table 'contact'`.

Moving all relationship data down (and thus duplicating it among all children) is obviously not a solution. Based on this small exploration it seems that bumping to PydanticV2 while using SQLModel requires a more significant rewrite of our models, at which point we probably should consider dropping `SQLModel` and instead go for a more explicit division between SQLAlchemy and Pydantic.

贡献指南

打开贡献指南

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。