Katedral Pillon
Katedral Pillon

Reputation: 14864

datastore does not show false boolean

I have a datastore "table" with multiple fields. One of the fields is a boolean called banned. I use it to ban users. Per my design, the field is never to be set except by a human moderator. But the thing is because no instance of the field is True, it is not present in the datastore. So I try to force the field to who by expressly setting it to false when a user creates an account. But of course that made no difference. So my question is this: How do I get app-engine datastore to show my boolean field even if it is false? Note that I just added the field to an existing database. And of course i am creating a new account just to see if the field would show up as false for the new account and the usual <missing> for the existing accounts. But nothing.

Upvotes: 0

Views: 461

Answers (2)

Konsol Labapen
Konsol Labapen

Reputation: 2434

Actually explicitly setting the boolean value to false is the correct approach. Therefore my guess is that the place where you are setting it to false is not executing. Maybe it's inside an if or other conditional that is not reached. So look into that.

A side note for Java developers. Normally it is not necessary to set a boolean to false since that is the default. But in this case you have to either set it or initialize it to false.

Upvotes: 0

Andrei Volgin
Andrei Volgin

Reputation: 41089

"because no instance of the field is True, it is not present in the datastore..."

If you set a property, the entity will have it. It does not matter if other entities have the same property or not. If you set a property and do not see it, there is an error somewhere.

"So I try to force the field... by expressly setting it to false..."

There is no need to do that. If "false" is a default setting, you don't need to store it. For example (using Datastore API):

if (user.isBanned()) {
    entity.setProperty("banned", true);
}

Boolean banned = (Boolean) entity.getProperty("banned");
user.setBanned(banned != null && banned);

This is especially useful if "banned" is an indexed property. Since, hopefully, the vast majority of your users are not banned, it will save you space as "banned" property - and an index for it - will be created only for banned users.

Upvotes: 2

Related Questions