pydantic-sqlalchemy
pydantic-sqlalchemy copied to clipboard
Fix Deprecation Issues and Update Code for Compatibility with Pydantic 2.0+ and SQLAlchemy 2.0+
Hello, I am Junbeom Lee
Thank you for your efforts in creating this valuable open-source library. I first discovered this library when ChatGPT recommended it for converting SQLAlchemy models to Pydantic models. I tried using it, but unfortunately, it requires Pydantic >2.0.0 and SQLAlchemy >2.0.0.
After checking, I noticed that Dependabot had attempted to update the dependencies but failed due to deprecation issues. To resolve this, I replaced the deprecated code. I hope this pull request reaches you soon! Please let me know if there is anything I should change.
Changes made 1: Replaced deprecated code with the new one
Pydantic:
-
Classes:
- Replaced
BaseConfigwithConfigDict
- Replaced
-
Methods:
- Replaced
PydanticUser.from_orm(user)withPydanticUser.model_validate(user) - Replaced
pydantic_user.dict()withpydantic_user.model_dump() - Replaced
PydanticUser.schema()withPydanticUser.model_json_schema()
- Replaced
-
Attributes:
- Replaced
orm_mode = Truewithfrom_attributes = True - Replaced
allow_population_by_field_name = Truewithpopulate_by_name = True
- Replaced
SQLAlchemy:
- Replaced
from sqlalchemy.ext.declarative import declarative_basewithfrom sqlalchemy.orm import declarative_base
Changes made 2: Test schemas
- Updated test schemas to reflect
default: Nonefor nullable fields.
@tiangolo It seems that the dependencies in pyproject.toml cannot be changed. I’ve tried several times to pass the tests, but either I didn’t get the label because I upgraded the version in pyproject.toml, or I couldn’t pass other tests because I didn’t have the latest versions of Pydantic or SQLAlchemy.
It would be nice to get this to work... I've been just putting an effort to use this as well
@bom1215 Thank you for providing this... Hope you can solve the tests...
Hello there, you can see more of what I think here in the README: https://github.com/tiangolo/pydantic-sqlalchemy?tab=readme-ov-file#-warning-use-sqlmodel-instead-
This change would remove support for Pydantic v1 and older SQLAlchemy versions, for which this project was made. For any new app I would recommend SQLModel instead.
Also, there are conflicts, and the tests are failing, so I'll pass on this, but thanks for the interest! :coffee: