Default value for True/False (boolean) fields is None

TL;DR - True/False fields in the Anvil Data Tables service have a default value of None when a row is added without the field value being set explicitly.

I spent a bit of time scratching my head today because I couldn’t get any rows returned for this:

waiting_users = app_tables.users.search(confirmed_email=True, enabled=False, admin_notified=False)

admin_notified is an extra column I added to the Users table, and keeps track of whether I have sent an email to admins to let them know that someone’s account is waiting to be authorised. I don’t set a value in it until after I’ve sent an email, when I change it to True.

I found that if I removed admin_notified from the search query, I got rows returned fine - but with it present I didn’t get any.

waiting_users = app_tables.users.search(confirmed_email=True, enabled=False)

Eventually I worked out that because I wasn’t explicitly setting the value of the field (in particular because the row is created by the Users service, not by my code), the value of the admin_notified field was actuall None.

Adding admin_notified=None to the search query returned me the right rows :slight_smile:

waiting_users = app_tables.users.search(confirmed_email=True, enabled=False, admin_notified=None)

I’ve checked with @daviesian and this is by design, but it isn’t specifically mentioned in the docs so I thought I’d post this here to hopefully save others a few minutes of time :slight_smile:

EDIT: Has since been pointed out that this is duplicated with this existing thread Uninitialized booleans in Data Tables

Yeah, it was discussed here, too :

Personally, I think it should default to one or the other (False is my vote). I don’t think “None” is a boolean state, at least not a useful one.

1 Like

Ah - thanks - I couldn’t find it referenced in previous threads when I searched the forum so thought it worth making it easily discoverable for others.

I am inclined to agree re: the default value, but equally can understand the design decision (defaulting to False implies a certainty of state which might not be an accurate reflection) but if it remains None it is worth updating the docs.

Certainly my subconcious assumed it would default to False and I had to heave myself out of that assumption to work out what was going on :slight_smile:

In the IDE you have a checkbox which is unchecked by default. To me that says False not none.

It’s a personal thing, but hardly serious :slight_smile:

It’s ultimately an acceptable choice to have an uninitialized value of None, but the fact that it is not documented even here: Anvil | Reference means it is a surprise, and surprises waste developer time debugging unexpected behavior. It should be false (common-sense default that any one would expect from appearances), and if there is a scenario where None is better, then should allow configuration of a True/False column to set None as the default, uninitialized value.

1 Like

Sorry to revive an old thread, but I’m still confused. If I put a True/False field in a data table, and I look at the table in the IDE, there is an empty box in its row. And its value shows up as False. If I then click in the box, it checks and the sure enough the row value becomes True. But if I click again in the box, the box disappears and the value becomes None. This all works fine from a program, but how do you toggle the value of the checkbox to False in the IDE after it has been made True or None?

I’ve tried a right-click (i.e., to pop up a menu – no dice). I’ve tried clicking just outside of the checkbox (no effect). I’ve tried double-clicking. Nothing I did produced a False. Not sure what else to try.

I believe you’ve found a bug in the IDE. There are 3 distinct values for a boolean column:

  • None (default value for every column in program-created rows)
  • False
  • True

There is good reason for being able to set all three of them via the IDE.

Hi @jonathan.falk,

Thanks for pointing this out! @p.colbert was right - this was a bug in the IDE which has now been fixed :slight_smile:

2 Likes

Just a note. Now the Delete key makes it None in the IDE. (Took a little experimenting to find.)

1 Like