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

How to query by column == None #146

Open
ricardo8990 opened this issue Nov 30, 2020 · 3 comments
Open

How to query by column == None #146

ricardo8990 opened this issue Nov 30, 2020 · 3 comments
Assignees
Labels
more added when Josiah is waiting for more from op question

Comments

@ricardo8990
Copy link

I've seen in #71 that there is a rom.NOT_NULL. However, from the code I can see that it only works for OneToOne or ManyToOne columns.

Is there a way to query for any other column type?

In my case I'm trying to add a column deactivated_at of type DateTime. I want to filter all active items by querying when deactivated_at doesn't exists or current time it's lower than deactivated_at value. But I still can't get it done.

class Key(Model):
    ...
    deactivated_at = DateTime(index=True)

active_keys = Key.query.filter(deactivated_at=(dt.datetime.utcnow() + dt.timedelta(microseconds=1), None))

Thanks in advance

@josiahcarlson
Copy link
Owner

  1. Don't use a default of None.
  2. Use a default of some "epoch"

I like January 1, 1970, because it's a convenient DEFAULT_MISSING_DATE = datetime.datetime.utcfromtimestamp(0) global define (used via DateTime(index=True, default=DEFAULT_MISSING_DATE). But you can pick any arbitrary date that DateTime supports and is before your valid data range.

@josiahcarlson josiahcarlson self-assigned this Dec 1, 2020
@josiahcarlson josiahcarlson added more added when Josiah is waiting for more from op question labels Dec 1, 2020
@ricardo8990
Copy link
Author

I get there are other ways to implement it. But I think would be a good feature request to be able to query if a column is not None. There're pretty common use cases.
In the case of SqlAlchemy you're able to query like: filter(deactivated_at!=None)

@josiahcarlson
Copy link
Owner

The problem: how to represent None in an inverted index, when historically we've not stored it, because None is missing data?

I will add an index_None=False option for columns, and if it is true, I will keep a separate "these entities store None" sets for every column. To ensure that this doesn't break folks weirdly, we'll allow duplicate None values in the case where index_None=True, unique=True.

That should solve your problem, and hopefully I'll get to it some time in the next week or two.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
more added when Josiah is waiting for more from op question
Projects
None yet
Development

No branches or pull requests

2 participants