Skip to content
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

Requires old SQLAlchemy syntax/deprecated methods. #60

Open
M3gar00 opened this issue Nov 27, 2024 · 1 comment
Open

Requires old SQLAlchemy syntax/deprecated methods. #60

M3gar00 opened this issue Nov 27, 2024 · 1 comment

Comments

@M3gar00
Copy link

M3gar00 commented Nov 27, 2024

Is there work being done/are there plans to update these Mixins to work with current SQLAlchemy?

Assume I have a table:

class MyData(Base, SerializerMixin):
    id: int = Column(Integer, primary_key=True)
    data: str = Column(String(length=255))
    other_data: Decimal = Column(Numeric(6, 3), default=Decimal(20.000))

And I want to retrieve some data from the database in that table, so I use current SQLAlchemy best practices:

with Session(engine(), future=True) as db:
    results = db.execute(select(MyData)).all()

Attempting to use .to_dict() will fail with the error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: Could not locate column in row for column 'to_dict'

If, instead, I use the deprecated method, I get success.

with Session(engine()) as db:
    result = db.query(MyData).all()
    list_dicts = [r.to_dict() for r in result]
@djnrrd
Copy link

djnrrd commented Dec 12, 2024

The SQLAlchemy best practices returns a Result object, containing Row objects when you use the .execute() method. You'll need to use the .scalar() or .scalars() methods to get the ORM objects you declared, which will then have the mixin methods you are looking for.

with Session(engine(), future=True) as db:
    results = db.execute(select(MyData)).scalars().all()

Or as a shorthand:

with Session(engine(), future=True) as db:
    results = db.scalars(select(MyData)).all()

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

No branches or pull requests

2 participants