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

A nested LocalStructuredProperty does not save in a way that can be read from Legacy NDB (proposal for a one-line fix included) #940

Open
DerekDuchesne opened this issue Jan 19, 2024 · 0 comments
Labels
api: datastore Issues related to the googleapis/python-ndb API.

Comments

@DerekDuchesne
Copy link

  1. API: python-ndb
  2. N/A
  3. Python 3.11.4
  4. google-cloud-ndb version: v2.2.2

Description

I have a model that looks like this:

class ChildLocalStructuredProperty(ndb.Model):
    str_field = ndb.StringProperty('s', indexed=False)

class ChildStructuredProperty(ndb.Model):
    local_structured_properties = ndb.LocalStructuredProperty(ChildLocalStructuredProperty, 'lp', repeated=True, indexed=False)

class Parent(ndb.Model):
    structured_property = ndb.StructuredProperty(ChildStructuredProperty, 'sp')

When saving the Parent entity, I found that the resulting properties would be:

[
    'sp.lp', (blobValue not readable by legacy NDB)
    'lp' (Array value readable by legacy NDB, but not prefixed correctly)
]

Possible fix

I traced the issue to _to_datastore() under class LocalStructuredProperty in google/cloud/ndb/model.py line 4497 where it sets data[self._name] = legacy_values:

if context.legacy_data:
    values = self._get_user_value(entity)
    if not self._repeated:
        values = [values]
    legacy_values = []
    for value in values:
        ds_entity = None
        if value is not None:
            ds_entity = _entity_to_ds_entity(value, set_key=self._keep_keys)
        legacy_values.append(ds_entity)
    if not self._repeated:
        legacy_values = legacy_values[0]
    data[self._name] = legacy_values        # <----- HERE

return keys

I believe it should be data[prefix + self._name] so that the legacy value is stored under the right property name including the prefix.

Thanks!

@product-auto-label product-auto-label bot added the api: datastore Issues related to the googleapis/python-ndb API. label Jan 19, 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

No branches or pull requests

1 participant