=== modified file 'includes/common.inc'
--- includes/common.inc	2009-05-24 17:39:30 +0000
+++ includes/common.inc	2009-05-24 23:09:17 +0000
@@ -4216,7 +4216,7 @@ function drupal_write_record($table, &$o
     }
 
     // For inserts, populate defaults from schema if not already provided.
-    if (!isset($object->$field) && empty($primary_keys) && isset($info['default'])) {
+    if (!property_exists($object, $field) && empty($primary_keys) && isset($info['default'])) {
       $object->$field = $info['default'];
     }
 
@@ -4227,7 +4227,7 @@ function drupal_write_record($table, &$o
     }
 
     // Build arrays for the fields and values in our query.
-    if (isset($object->$field)) {
+    if (isset($object->$field) || (property_exists($object, $field) && $info['not null'] == FALSE)) {
       if (empty($info['serialize'])) {
         $fields[$field] = $object->$field;
       }

=== modified file 'modules/simpletest/tests/common.test'
--- modules/simpletest/tests/common.test	2009-05-24 17:39:30 +0000
+++ modules/simpletest/tests/common.test	2009-05-24 23:09:17 +0000
@@ -759,15 +759,34 @@ class DrupalDataApiTest extends DrupalWe
     // Insert an object record for a table with a single-field primary key.
     $vocabulary = new stdClass();
     $vocabulary->name = 'test';
+    $vocabulary->description = '';
+    $vocabulary->help = NULL; // This field cannot be set to NULL.
     $insert_result = drupal_write_record('taxonomy_vocabulary', $vocabulary);
     $this->assertTrue($insert_result == SAVED_NEW, t('Correct value returned when a record is inserted with drupal_write_record() for a table with a single-field primary key.'));
     $this->assertTrue(isset($vocabulary->vid), t('Primary key is set on record created with drupal_write_record().'));
 
+    // Check the inserted values are correct.
+    $result = db_query("SELECT * FROM {taxonomy_vocabulary} WHERE vid = :vid", array(':vid' => $vocabulary->vid))->fetchObject();
+    $this->assertIdentical($result->name, 'test', t('Name field set.'));
+    $this->assertIdentical($result->description, '', t('Description field set.'));
+    $this->assertIdentical($result->relations, '0', t('Relations field set to default value.'));
+    $this->assertIdentical($result->help, '', t('Help field set to default value.'));
+
     // Update the initial record after changing a property.
-    $vocabulary->name = 'testing';
+    $vocabulary->name = FALSE; // This should set the field to a default value.
+    $vocabulary->description = NULL; // This field can be set to NULL.
+    $vocabulary->relations = TRUE; // This field should be cast to an int.
+    $vocabulary->help = 'help';
     $update_result = drupal_write_record('taxonomy_vocabulary', $vocabulary, array('vid'));
     $this->assertTrue($update_result == SAVED_UPDATED, t('Correct value returned when a record updated with drupal_write_record() for table with single-field primary key.'));
 
+    // Check the updated values are correct.
+    $result = db_query("SELECT * FROM {taxonomy_vocabulary} WHERE vid = :vid", array(':vid' => $vocabulary->vid))->fetchObject();
+    $this->assertIdentical($result->name, '', t('Name field set to an empty string.'));
+    $this->assertIdentical($result->description, NULL, t('Description field set to NULL.'));
+    $this->assertIdentical($result->relations, '1', t('Relations field set and type cast to int.'));
+    $this->assertIdentical($result->help, 'help', t('Help field set.'));
+
     // Insert an object record for a table with a multi-field primary key.
     $vocabulary_node_type = new stdClass();
     $vocabulary_node_type->vid = $vocabulary->vid;
@@ -852,4 +871,3 @@ class DrupalErrorCollectionUnitTest exte
     }
   }
 }
-

