Windows Azure SDK for PHP – ‘PartitionKey’ property value must satisfy is_string.

When building out an application there are many factors you need to consider, these considerations boil down to functional and non-functional requirements that you as a developer or architect are set out to achieve. In many circumstances, if not all, we are accepting data as an input from a user or other medium, transforming or normalizing that data into something that will help fulfill other requirements of an application.

In order to fulfill scalability goals it may be necessary to find alternative means to traditional storage, something that was designed with scalability in mind. One such service is Windows Azure Table Storage which is defined as:

[Windows Azure Storage] Tables offer NoSQL capabilities for applications that require storage of large amounts of unstructured data. Tables are an ISO 27001 certified managed service which can auto scale to meet massive volume of up to 100 terabytes and throughput and accessible from virtually anywhere via REST and managed API’s.

The topic of Windows Azure Storage Abstractions and their scalability targets is some what out of scope of this post, however, keep in mind that it is important to understand the Windows Azure Storage Architecture and more specifically How to get the most out of Windows Azure [Storage] Tables.

Integer values for PartitionKey or RowKey with PHP

If you’ve done your research on designing for scale with Windows Azure Storage and you find that Integer values are what you need for your specific data model, keep in mind that PartitionKey and RowKey values are of type string.

While this doesn’t seem like a foreign concept you may find yourself in a situation where you have an error while attempting to use integers as strings while interacting with the Windows Azure SDK for PHP. The error you will receive is “PartitionKey [or RowKey] property value must satisfy is_string”. In my particular scenario I was loading my tables by iterating through an array of key/value pairs.

My array looked like this:


In which you may expect the key to be treated as a string, which isn’t the case. To understand the reasoning behind this, we’ll go right to the source.

The key can either be an integer or a string. The value can be of any type.

Additionally the following key casts will occur:

  • Strings containing valid integers will be cast to the integer type. E.g. the key "8" will actually be stored under 8. On the other hand "08" will not be cast, as it isn’t a valid decimal integer.
  • Floats are also cast to integers, which means that the fractional part will be truncated. E.g. the key 8.7 will actually be stored under 8.
  • Bools are cast to integers, too, i.e. the key true will actually be stored under 1 and the key false under 0.
  • Null will be cast to the empty string, i.e. the key null will actually be stored under "".
  • Arrays and objects can not be used as keys. Doing so will result in a warning: Illegal offset type.

According to the documentation, you’ll notice that “strings containing valid integers will be cast to integer type”, in order to ensure that the now converted integer is provided to the service proxy as a string, we need to call strval() on the key when providing it as a PartitionKey or RowKey. To provide an example of this consider the following:


Where addEntity() constructs an entity and inserts it into Windows Azure Table Storage.

Conclusion

In this post I clarified a potential issue that may arise when iterating over an array with key/value pairs to push entities into Windows Azure Table Storage.

Stay Cloudy My Friends…