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

fix: enable filtering on UserProperty fields #1004

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

yihaoWang
Copy link

Description

This PR fixes an issue where filtering queries on UserProperty fields was not working. The root cause was that the _MEANING_PREDEFINED_ENTITY_USER meaning was not being properly set at the entity's top level during query filter creation.

In the Google Datastore, UserProperty meanings are stored at the entity's top level rather than within individual property values. This PR adds the necessary handling to ensure the meaning is correctly propagated when creating filters for UserProperty fields.

Changes

  • Added _comparison method to UserProperty class to set the proper meaning during filter creation
  • Added documentation to clarify the special handling of UserProperty meanings at the entity level

Testing

To reproduce and verify the fix:

import os
import sys
from google.appengine.api import users
from google.cloud import ndb

class TestModel(ndb.Model):
    owner = ndb.UserProperty()


def init_ndb():
    os.environ["AUTH_DOMAIN"] = "ikala.ai"
    
    ndb_client = ndb.Client(project="cloud-sa-sandbox-1")
    return ndb_client


def test_get_user_data():
    ndb_client = init_ndb()
    with ndb_client.context():
        user = users.User(email="[email protected]")
        test_model = TestModel(owner=user)
        test_model.put()

        # Before the fix: This query would not work properly
        # After the fix: This query correctly filters by user
        query = TestModel.query().filter(TestModel.owner == user)
        result = query.get()


if __name__ == "__main__":
    test_get_user_data()

Fix UserProperty query functionality by properly handling meanings at the
entity's top level. Previously, UserProperty fields could not be used in
queries because the _MEANING_PREDEFINED_ENTITY_USER was not being set
correctly in the outer layer of the property.
@yihaoWang yihaoWang requested review from a team as code owners December 1, 2024 03:44
Copy link

google-cla bot commented Dec 1, 2024

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@product-auto-label product-auto-label bot added the api: datastore Issues related to the googleapis/python-ndb API. label Dec 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: datastore Issues related to the googleapis/python-ndb API.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant