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

Enhancement: make .get_one respect None filters #187

Open
funkindy opened this issue May 6, 2024 · 4 comments
Open

Enhancement: make .get_one respect None filters #187

funkindy opened this issue May 6, 2024 · 4 comments
Labels
enhancement New feature or request

Comments

@funkindy
Copy link

funkindy commented May 6, 2024

Summary

I use soft delete for my models, and i try to get instance with repo like this:

obj = await repo.get_one(id='id', deleted_at=None)

because i dont want instance to be selected if it is deleted.

The problem is that with these kwargs i dont get any object at all, because the filter doesn't get converted to deleted_at is null for the db query but to deleted_at = 'None'

Is it as intended? For now the workaround to this is to pass custom statement to get_one, but its not very convenient.
Thank you.

Basic Example

No response

Drawbacks and Impact

No response

Unresolved questions

No response

@funkindy funkindy added the enhancement New feature or request label May 6, 2024
@peterschutt
Copy link
Contributor

Does this work?

from sqlalchemy import null

obj = await repo.get_one(id='id', deleted_at=null())

@cofin
Copy link
Member

cofin commented May 7, 2024

You can also do something like this:

from sqlalchemy import null

obj = await repo.get_one(MyModel.deleted_at.is_(None), id='id')

Depending on the data type used, the item.field = None will automatically convert to the IS NULL/IS NOT NULL syntax. But, not all work like this (or at least that's what I am currently remembering)

@funkindy
Copy link
Author

funkindy commented May 7, 2024

from sqlalchemy import null

No, under the hood FilterableRepository._filter_by_where so i even cant render the statement in the debugger:

sqlalchemy.exc.CompileError: Could not render literal value "<sqlalchemy.sql.elements.Null object at 0x1075f13a0>" with datatype DATETIME; see parent stack trace for more detail.

Looks like SA tries to convert this explicilty into DATETIME:

 WHERE user.id = $1::VARCHAR AND user.deleted_at = $2::TIMESTAMP WITH TIME ZONE]

@funkindy
Copy link
Author

funkindy commented May 7, 2024

You can also do something like this:

from sqlalchemy import null

obj = await repo.get_one(MyModel.deleted_at.is_(None), id='id')

Depending on the data type used, the item.field = None will automatically convert to the IS NULL/IS NOT NULL syntax. But, not all work like this (or at least that's what I am currently remembering)

Yes, this is exactly my current workaround. But it would be nice to have it in kwargs either with None or null()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants