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

Issues getting DateTimeProperty() auto_now_add and auto_now attributes to work #137

Closed
JustasCe opened this issue Jul 5, 2019 · 3 comments
Assignees
Labels
priority: p1 Important issue which blocks shipping the next release. Will be fixed prior to next release. 🚨 This issue needs some love. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.

Comments

@JustasCe
Copy link

JustasCe commented Jul 5, 2019

I am having problems getting auto_now_add and auto_now to work.
As I understand it should assign appropriate values on inserts and and updates of the entity, I don't have to pass any values to it manually.

However I cannot get it to work.

Example when trying to create an Entity, put it to datastore and retrieve the same Entity based on it's key that was returned during the put()

import logging

from google.cloud.ndb import (
    Client,
    DateTimeProperty,
    Model,
    StringProperty,
)

logging.basicConfig(level=logging.DEBUG)


class User(Model):
    first_name = StringProperty()
    created_at = DateTimeProperty(indexed=True, auto_now_add=True)
    updated_at = DateTimeProperty(indexed=True, auto_now=True)


ndb_client = Client(project="project-name", namespace="ndb_tests")

with ndb_client.context():
    logging.debug(f"Putting a new user to a database")
    user_key = User(
        first_name="MyName",
    ).put()
    logging.debug(f"Retrieving data based on the returned user key: {user_key}")
    user = user_key.get()
    logging.debug(f"Retrieved user data: {user}")

Debug output

DEBUG:root:Putting a new user to a database
DEBUG:google.cloud.ndb._datastore_api:Commit(mode: NON_TRANSACTIONAL
mutations {
  upsert {
    key {
      partition_id {
        project_id: "project-name"
        namespace_id: "ndb_tests"
      }
      path {
        kind: "User"
      }
    }
    properties {
      key: "created_at"
      value {
        null_value: NULL_VALUE
      }
    }
    properties {
      key: "first_name"
      value {
        string_value: "MyName"
      }
    }
    properties {
      key: "updated_at"
      value {
        null_value: NULL_VALUE
      }
    }
  }
}
project_id: "project-name"
)
DEBUG:google.cloud.ndb._datastore_api:timeout=None
DEBUG:google.auth.transport.requests:Making request: POST https://oauth2.googleapis.com/token
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): oauth2.googleapis.com:443
DEBUG:urllib3.connectionpool:https://oauth2.googleapis.com:443 "POST /token HTTP/1.1" 200 None
DEBUG:google.cloud.ndb._datastore_api:mutation_results {
  key {
    partition_id {
      project_id: "project-name"
      namespace_id: "ndb_tests"
    }
    path {
      kind: "User"
      id: 5739956077068288
    }
  }
  version: 1562351307488774
}
index_updates: 7

DEBUG:root:Retrieving data based on the returned user key: Key('User', 5739956077068288, namespace='ndb_tests')
DEBUG:google.cloud.ndb._datastore_api:Lookup(read_options {
}
keys {
  partition_id {
    project_id: "project-name"
    namespace_id: "ndb_tests"
  }
  path {
    kind: "User"
    id: 5739956077068288
  }
}
project_id: "project-name"
)
DEBUG:google.cloud.ndb._datastore_api:timeout=None
DEBUG:root:Retrieved user data: User(key=Key('User', 5739956077068288, namespace='ndb_tests'), created_at=None, first_name='MyName', updated_at=None)

As you can see from the logs created_at and updated_at values are both None even though both of the fields had either auto_now_add or auto_now set to True.

@cguardia
Copy link
Contributor

cguardia commented Jul 6, 2019

This is a bug. The _prepare_for_put method, which should set the dates, is never called. Will take a look.

@cguardia cguardia self-assigned this Jul 6, 2019
@cguardia cguardia added 🚨 This issue needs some love. labels Jul 6, 2019
@cguardia
Copy link
Contributor

cguardia commented Jul 9, 2019

Fixed, via #138.

@cguardia cguardia closed this as completed Jul 9, 2019
@cguardia
Copy link
Contributor

cguardia commented Jul 9, 2019

Thanks a lot for the bug report!

@yoshi-automation yoshi-automation added the 🚨 This issue needs some love. label Apr 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority: p1 Important issue which blocks shipping the next release. Will be fixed prior to next release. 🚨 This issue needs some love. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.
Projects
None yet
Development

No branches or pull requests

3 participants