pydantic-sqlalchemy icon indicating copy to clipboard operation
pydantic-sqlalchemy copied to clipboard

Fix Deprecation Issues and Update Code for Compatibility with Pydantic 2.0+ and SQLAlchemy 2.0+

Open bom1215 opened this issue 9 months ago • 1 comments

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 BaseConfig with ConfigDict
  • Methods:

    • Replaced PydanticUser.from_orm(user) with PydanticUser.model_validate(user)
    • Replaced pydantic_user.dict() with pydantic_user.model_dump()
    • Replaced PydanticUser.schema() with PydanticUser.model_json_schema()
  • Attributes:

    • Replaced orm_mode = True with from_attributes = True
    • Replaced allow_population_by_field_name = True with populate_by_name = True

SQLAlchemy:

  • Replaced from sqlalchemy.ext.declarative import declarative_base with from sqlalchemy.orm import declarative_base

Changes made 2: Test schemas

  • Updated test schemas to reflect default: None for nullable fields.

bom1215 avatar Mar 01 '25 07:03 bom1215

@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.

bom1215 avatar Mar 01 '25 08:03 bom1215

It would be nice to get this to work... I've been just putting an effort to use this as well

marcellodesales avatar Mar 12 '25 14:03 marcellodesales

@bom1215 Thank you for providing this... Hope you can solve the tests...

marcellodesales avatar Mar 12 '25 14:03 marcellodesales

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:

tiangolo avatar Jun 11 '25 20:06 tiangolo