I have a long varchar field that I'm using to save serialised data with drupal_write_record. I'd like to set the default value of the column to "a:0:{}" (which is serialize(array())).

However, when Drupal runs database updates, they are run through the update_sql() function which includes the default value in the query. This means that the '{' and '}' are interpreted as prefixing table names and removed, leaving the actual value of "a:0:" in the database, which is not what I want.

Is there a way to use curly braces in a field spec's "default" value?

  • May you show the code you use to update the database schema?
    – apaderno
    Commented Dec 27, 2011 at 16:41

3 Answers 3


In your schema, for the field you want to serialize, you can add the key serialize => TRUE and use the drupal_write_record() function to write the field in the database. After, you have just to send a simple array()(without serialize()) and Drupal will do it by himself.

You can have more information in the documentation.

  • I am already doing this. You do have to serialise the value yourself when you define the field in your schema and database update function, e.g.: $spec = array('default' => serialize(array()), 'type' => 'varchar', ...); And yes, it works correctly for use from drupal_write_record... the real question is how to specify the default value that is compatible with that. Commented Sep 28, 2011 at 5:51
  • Ok I see, like you added on drupal.org it's a real bug, even in Drupal 7 and I didn't check but I guess it's the same in Drupal 8.
    – yvan
    Commented Sep 28, 2011 at 7:32

If you use db_query, you can do something like this, normally only the curly braces inside the SQL string are replaced.

db_query("UPDATE {mytable} SET myfield = :data", array(':data' => "a:0:{}"));
  • What I was really after was a way to specify it in the schema using drupal schema api, so that the module could be deployed, etc. Commented Sep 3, 2012 at 9:38
  • We normally never provide a default for serialized fields, why do you need to do it?
    – Attiks
    Commented Sep 3, 2012 at 9:44
  • Why do you ever have default data? Why do you ever have serialized fields? They are both valid, independently of each other. A default value can be set in the schema, and it works, so long as there is no curly braces in them. Commented Sep 3, 2012 at 9:52
  • I think you misunderstood me, we use default values if we can and if it makes sense, but I never saw Drupal core and/or any other module use a default for serialized data. I agree it would be nice if you could specify a default, but AFAIK it isn't necessary.
    – Attiks
    Commented Sep 3, 2012 at 10:02
  • Best thing todo is to open an issue (bug report or feature request) against Drupal 8 core, so it might get fixed.
    – Attiks
    Commented Sep 3, 2012 at 10:03

There is already a bug report for Drupal 6, Impossible to insert serialized data with update_sql(), but the bug was never fixed.

The issue is only for Drupal 6, as Drupal 7 doesn't use anymore update_sql(). The only workaround is using your own function, instead of update_sql().

function mymodule_update_sql($sql) {
  $args = func_get_args();

  $result = db_query($sql, $args);
  $sql = mymodule_query_get_string($sql, $args);
  return array('success' => $result !== FALSE, 'query' => check_plain($sql));

function mymodule_query_get_string($query) {
  $args = func_get_args();
  $query = db_prefix_tables($query);

  if (isset($args[0]) and is_array($args[0])) {
    // All arguments are in one array.
    $args = $args[0];
  _db_query_callback($args, TRUE);
  $query = preg_replace_callback(DB_QUERY_REGEXP, '_db_query_callback', $query);
  return $query;

You can use mymodule_update_sql() in the same way you use db_query(), as it can use placeholders.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.