Skip to content

Datastore - Queries created by Transaction don't fall back to Datastore namespace #2447

@beaulac

Description

@beaulac

Queries created in a transaction without an explicitly specified namespace do not fall back to the datastore's constructor-specified namespace.

Because of this, the namespace must always be explicitly specified for ancestor queries created within a transaction.

This is probably not desired behaviour, since the docs for Transaction#createQuery provide a namespace-less example (transaction.createQuery('Company');)

Steps to reproduce

const ds = require('@google-cloud/datastore')({namespace: 'myNamespace', projectId: 'my-project'});

const ancestorKey = ds.key(['ancestorKind', 1234]);

// This is OK, query namespace matches key namespace:
ds.createQuery('childKind')
  .hasAncestor(ancestorKey)
  .run()
  .then(([results]) => results);

ds.transaction().run()
  .then(([activeTransaction]) => {

// This fails! 
// "Error: The query namespace is  but ancestor namespace is myNamespace"
    activeTransaction.createQuery('childKind')
             .hasAncestor(ancestorKey)
             .run()
             .then(([results]) => results);
  });

Cause

This occurs because Transaction binds this to itself when calling datastore's createQuery:

Transaction.prototype.createQuery = function() {
  return this.datastore.createQuery.apply(this, arguments);
};

Then, in createQuery:

  if (arguments.length < 2) {
    kind = namespace;
    namespace = this.namespace;
  }

Unfortunately this.namespace is undefined 😭

This could be fixed in the Transaction constructor:

function Transaction(datastore) {
  this.datastore = datastore;
// Add this line to match datastore's fallback namespace:
  this.namespace = datastore.namespace;

Environment details

Probably not too relevant, but:

  • OS: macOS 10.12.5
  • Node.js version: 6.10.2
  • npm version: 5.1.0
  • google-cloud/datastore version: 1.0.2

Metadata

Metadata

Labels

api: datastoreIssues related to the Datastore API.type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions