AddCodedValueToDomain, how to add an empty string value

450
6
06-01-2019 01:11 PM
JonHall
Occasional Contributor II

Here's a code snippet we're using to create a domain, and add values to it, for the purpose of constraining street name prefix directionals:

arcpy.CreateDomain_management(GeodatabaseName, "StreetNameDirectional", "prefix directional of parsed street name", "TEXT", "CODED")
domDict = {"N":"North", "S": "South", "E": "East", "W": "West", "NE": "Northeast", "NW": "Northwest", "SE": "Southeast", "SW": "Southwest"}
for code in domDict:
arcpy.AddCodedValueToDomain_management(GeodatabaseName, "StreetNameDirectional", code, domDict
)

In ArcMap's Table interface, while editing a row, the 8 values and descriptions appear in the combo-box list for fields that have that domain applied. So far, so good.

However, after a row has a value of "N" in that field, there is no way to change it from "N" back to none, i.e. an empty string, using the combo-box list

I can use the Field Calculator to set it equal to "", an empty string, but that's not an intuitive solution. I want an empty string value with a description of "none".

In the ArcCatalog tree, Database Properties, Domain tab, I tried adding a 9th row to the StreetNameDirectional domain with no value, and Description = "None". Click Apply, looks okay, close ArcCatalog, and there's no 9th row of domain values & descriptions. 

Adding two double-quotes as the 9th row Value for the domain, with Description = "None", only succeeds in adding a pair of double-quote ASCII characters in the prefix directional field.

I want to do this programmatically, using arcpy.AddCodedValueToDomain_management, but I can't even do it through the ArcCatalog user interface. I can't find anything documented RE: including an empty string in a coded value domain. 

I should add a related annoyance, that's more trivial: The field is nullable, so "<Null>" appears at the top of the combo-box list.  I don't want the user to choose a null value, I want an empty string value; and ArcMap won't let you select <Null> from the list, throwing an error that "This field will only store string values with up to 2 character(s)". 

Thanks in advance for any assistance you may provide.

6 Replies
RandyBurton
MVP Regular Contributor

I don't think it is possible to have a coded value domain where the "code" is None/Null.  To have an option of "<Null>" show up in the domain selections (where the actual code stored is a NULL value), you need to set the field using the domain to "Allow NULL Values".

directional domain <Null> examplefield properties

Typically, I set the default value of my fields using domains to use a code like "?" or "NA".  These would then display a description of "None", "Missing" or a prompt to "Make a selection".  I would then set allow null values to "No".  I would set any fields with a null to this default value.

Hope this helps.

JonHall
Occasional Contributor II

Randy, thanks for the response. Yes, "I don't think it is possible to have a coded value domain where" the code is an empty string.  That's why I posted this question.

ESRI hasn't documented whether it is or isn't possible, but trial and error indicates that it's not.

I suspect that our workgroup has mis-used the isNullable option, with the intention of allowing empty values. A field that isNullable can be deleted from the attribute table.  The field is REQUIRED, whether it contains values, or not.

My understanding is that a database null value is quite different than storing a 6-characer string, "<Null>", which is what ESRI's ArcMap table user interface is attempting, when it throws the error that "This field will only store string values with up to 2 character(s)".  I may start a separate thread on that topic, but hate wasting time spitting into the wind, complaining about ESRI software, unless it's a stupid-user-problem and I might learn the error of my ways from the GIS community.  If I solve either issue, I'll post the workaround or solution.

0 Kudos
RandyBurton
MVP Regular Contributor

We may be in agreement, but to be clear, "<Null>" is not a six-character string or the description of a coded value.  It is the file geodatabase's representation that a null value is in the field.  When it appears in a drop-down list, like a display of a database table in edit mode or an application like Collector, when "<Null>" is selected, a null value will be placed in the field.

0 Kudos
JonHall
Occasional Contributor II

Right, Randy, that's the behavior I expected, but ArcMap throws that error

message, and no null value is saved in the field.

0 Kudos
RandyBurton
MVP Regular Contributor

What type of geodatabase are you working with?

0 Kudos
JonHall
Occasional Contributor II

The file geodatabase schema, created with arcpy script, defining the fields

and domains, has been loaded into enterprise SDE on SQL Server.

0 Kudos