Skip to content

SQLAlchemy deprecation in SqlAlchemyDictTypeTest #486

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

hammerhead
Copy link
Member

Summary of the changes / Why this is an improvement

@amotl, this is an attempt at fixing the deprecation warning in SqlAlchemyDictTypeTest. I'm not 100% sure about the test_update_with_dict_column, this would need manual verification.

Checklist

  • CLA is signed

@hammerhead hammerhead requested a review from amotl December 20, 2022 11:03
values({
"data['x']": "Trillian"
})
values({ mytable.c.data['x']: "Trillian" })
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks to be valid:

from crate.client.sqlalchemy import types
from sqlalchemy.orm import Session
import sqlalchemy as sa

engine = sa.create_engine("crate://")
metadata_obj = sa.MetaData()

mytable = sa.Table(
    "characters",
    metadata_obj,
    sa.Column("id", sa.String, primary_key=True),
    sa.Column("details", types.Object),
    autoload_with=engine,
)

session = Session(bind=engine)
session.execute(
    mytable.update()
    .where(mytable.c.id == "1")
    .values({mytable.c.details["x"]: "Trillian"})
)

Resulting query as per sys.jobs_log: UPDATE characters SET details['x'] = ? WHERE characters.id = ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The previous test might have been wrong, using values({"data['x']": "Trillian"}) throws an exception in a real-world example without any mocking: TypeError: Object of type BindParameter is not JSON serializable

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh. Thanks for spotting and investigating diligently.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

throws an exception in a real-world example without any mocking

So, if that did not work before anyway, we will not have to announce anything as "breaking change", right? When looking at this again, I bet this was the spot which may have tripped me in the past already. Thanks again!

Copy link
Member

@amotl amotl Dec 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added 2c6db67 to make the tests succeed on my end.

@hammerhead hammerhead force-pushed the hammerhead/sqlalchemy-deprecations branch 2 times, most recently from c044ede to d5cb639 Compare December 20, 2022 14:00
hammerhead and others added 2 commits December 20, 2022 23:23
Don't use `autoload_with` on table `mytable`, because it will never be
persisted to a database, so it can not be inspected.
@amotl amotl force-pushed the hammerhead/sqlalchemy-deprecations branch from d5cb639 to 2c6db67 Compare December 20, 2022 22:24
@amotl amotl merged commit 48bc63e into amo/sa-mitigate-deprecation-warnings Dec 20, 2022
@amotl amotl deleted the hammerhead/sqlalchemy-deprecations branch December 20, 2022 22:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants