 includes/common.inc                  |   14 +++++++++-----
 modules/simpletest/tests/common.test |    5 +++++
 2 files changed, 14 insertions(+), 5 deletions(-)

diff --git a/includes/common.inc b/includes/common.inc
index f95597f..f4c08ef 100644
--- a/includes/common.inc
+++ b/includes/common.inc
@@ -6801,6 +6801,7 @@ function drupal_write_record($table, &$record, $primary_keys = array()) {
 
   $object = (object) $record;
   $fields = array();
+  $default_fields = array();
 
   // Go through the schema to determine fields to write.
   foreach ($schema['fields'] as $field => $info) {
@@ -6823,6 +6824,7 @@ function drupal_write_record($table, &$record, $primary_keys = array()) {
     if (!property_exists($object, $field)) {
       // Skip fields that are not provided, default values are already known
       // by the database.
+      $default_fields[] = $field;
       continue;
     }
 
@@ -6853,10 +6855,6 @@ function drupal_write_record($table, &$record, $primary_keys = array()) {
     }
   }
 
-  if (empty($fields)) {
-    return;
-  }
-
   // Build the SQL.
   if (empty($primary_keys)) {
     // We are doing an insert.
@@ -6873,10 +6871,16 @@ function drupal_write_record($table, &$record, $primary_keys = array()) {
         unset($fields[$serial]);
       }
     }
-    $query = db_insert($table, $options)->fields($fields);
+    // Create an INSERT query. useDefaults() is necessary for the SQL to be
+    // valid when $fields is empty.
+    $query = db_insert($table, $options)
+      ->fields($fields)
+      ->useDefaults($default_fields);
+
     $return = SAVED_NEW;
   }
   else {
+    // Create an UPDATE query.
     $query = db_update($table)->fields($fields);
     foreach ($primary_keys as $key) {
       $query->condition($key, $object->$key);
diff --git a/modules/simpletest/tests/common.test b/modules/simpletest/tests/common.test
index a643ff9..a83ea86 100644
--- a/modules/simpletest/tests/common.test
+++ b/modules/simpletest/tests/common.test
@@ -1877,6 +1877,11 @@ class DrupalDataApiTest extends DrupalWebTestCase {
    * Test the drupal_write_record() API function.
    */
   function testDrupalWriteRecord() {
+    // Insert a record with no columns populated.
+    $record = array();
+    $insert_result = drupal_write_record('test', $record);
+    $this->assertTrue($insert_result == SAVED_NEW, t('Correct value returned when an empty record is inserted with drupal_write_record().'));
+
     // Insert a record - no columns allow NULL values.
     $person = new stdClass();
     $person->name = 'John';
