aiondemand / aiondemand/AIOD-rest-api

Migration to Pydantic V2

Abierto
#573 0 comentarios 0 reacciones 0 asignados Ver en GitHub
wontfix
Lenguaje dominante
Python
Estrellas
36
Forks
77
Métricas de merge de PR
Sin PR fusionados en 30 d

Descripción

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.

Guía de contribución

Abrir la guía de contribución

Evaluación

Este issue todavía no se ha evaluado.

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.