How to make a filter query across all tenants ignoring the namespace of each tenant (i.e. a query with WILDCARD namespace ) in objectify

94 views
Skip to first unread message

Baqir Rizvi

unread,
Nov 4, 2016, 3:00:40 AM11/4/16
to objectify-appengine

I have a multitenant application in which each school is a tenant. Each school can have many teachers. So I have two kind of entity one is "School" and second is "Teacher" such that each teacher has a parent school. I am using @Parent annotation of objectify for building the relationship between school and teacher.

Now I want to count the number of teachers across the schools for my admin portal.Is there any concept of global namespace in objectify/datastore?

Jeff Schnitzer

unread,
Nov 4, 2016, 1:00:20 PM11/4/16
to objectify-appengine
This is the problem with namespaces, and why I tell everyone: Don’t use them, ever. Just model your data normally. If you want to query the teachers for a school, you can do that without namespaces. On the other hand, as soon as you want to do a cross-namespace query, you’re screwed.

Jeff

On Fri, Nov 4, 2016 at 12:00 AM, Baqir Rizvi <baqir.g...@gmail.com> wrote:

I have a multitenant application in which each school is a tenant. Each school can have many teachers. So I have two kind of entity one is "School" and second is "Teacher" such that each teacher has a parent school. I am using @Parent annotation of objectify for building the relationship between school and teacher.

Now I want to count the number of teachers across the schools for my admin portal.Is there any concept of global namespace in objectify/datastore?

--
You received this message because you are subscribed to the Google Groups "objectify-appengine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to objectify-appengine+unsub...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Baqir Rizvi

unread,
Nov 7, 2016, 12:47:12 AM11/7/16
to objectify-appengine, je...@infohazard.org
Thanks for replying Jeff. When you do things by yourself then things become more clear with pros and cons. I wonder why there is no concept of global namespace.
Currently I am running queries on each namespace. one query for getting all the namespaces and then multiple queries for getting teacher in that namespace. Assuming there are 100 tenants(school) , in worst case I need to make 1+100 query , if I am not wrong. Assuming there are many other things that my admin portal need to show which requires these kind of queries, there are following questions-
questions:
  1. what possible issues these kind methods(queries on each namespace) can results, in terms of performance, leakage, security etc(anything else that I don't know).
  2. Is there any other method solve this problem assuming that multitenancy is enable.

Jeff Schnitzer

unread,
Nov 8, 2016, 11:08:08 AM11/8/16
to objectify-appengine
Just put an indexed Key<?> field “school” on your Teacher. Then query teachers filtering by school (or not, if you want a global query). Namespaces are madness. N queries will perform horribly.

Pretend the namespace feature does not exist. It was a terrible idea for exactly this reason.

Jeff

To unsubscribe from this group and stop receiving emails from it, send an email to objectify-appengine+unsubscribe...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Chuck Wight

unread,
Apr 25, 2024, 7:19:26 AMApr 25
to objectify-appengine
Jeff - Thanks for the sage advice: Don't use namespaces, ever.
The ONE attractive feature of namespaces is that it guarantees uniqueness because two (or more) properties are combined into a single key. What is the best way to guarantee uniqueness if the entities are modeled normally? That is, how should I best prevent accidental creation of a duplicate school/teacher entity?

Jeff Schnitzer

unread,
Apr 25, 2024, 10:07:59 PMApr 25
to objectify...@googlegroups.com
In the olde days you had to either use a unique natural key, or create a second entity whose primary key is the unique natural key (ie id: "school-teacher" or even {parent: "theschool", id: "theteacher"}).

However, datastore queries are now strongly consistent. So in a transaction, query for the pre-existence of your duplicate (the traditional way) and if it already exists, don't create it. Just like a normal RDBMS.

Jeff

To unsubscribe from this group and stop receiving emails from it, send an email to objectify-appen...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/objectify-appengine/ec5289fb-b652-4299-855e-bbf453e8a4dan%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages