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

Missing 2.0 style ORM functions #331

Open
jonyscathe opened this issue Mar 31, 2023 · 0 comments
Open

Missing 2.0 style ORM functions #331

jonyscathe opened this issue Mar 31, 2023 · 0 comments
Labels
bug Something isn't working

Comments

@jonyscathe
Copy link
Contributor

jonyscathe commented Mar 31, 2023

Describe the bug
After @seano314's update to support sqlalchemy 2.0 I have had success migrating a lot of my code and test code over to 2.0 style form following the migration guide.

The one thing I have struggled with is with the use of scalar()
According to the migration guide, simple queries for a single value should be changed from something like:

session.query(User).count()

to

session.scalar(
  select(func.count()).
  select_from(User)
)

# or

session.scalar(
  select(func.count(User.id))
)

Currently I don't believe that that new style method of using count() can be mocked using mock-alchemy as it currently stands.
And I'm 100% not sure that this is an easy fix with scalar() in 2.0 now being able to be the start and end of a chain rather than just the end of a chain.
If anyone has any good ideas on how to fix this I am happy to make a PR. But this isn't quite as simple

To Reproduce
Try mock data for something like:

session.scalar(
  select(func.count(User.id))
)

Expected behavior
For it to be possible to make a UnifiedAlchemyMagicMock with data set such that session.scalar() calls can be mocked correctly.

Screenshots
If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

  • OS: [Ubuntu]
  • Version [0.2.6]

Additional context
Another aspect of the new ORM that isn't yet supported is scalar_one though I believe that one is rather easy to fix as it is essentially the same as one.
And the other one is using scalars() as the beginning of a chain as in:

session.scalars(
  select(User)
).all()

However it is easy enough to get the same thing with:

session.execute(
  select(User)
).scalars().all()

So that one isn't super urgent.

As a workaround for now, it is possible to run scalar as something like:

session.execute(
 select(func.count(User.id))
).scalar()

But given that is not the suggested method in the sqlalchemy docs I'd like to be able to use session.scalar() directly

@jonyscathe jonyscathe added the bug Something isn't working label Mar 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant