=== modified file 'includes/common.inc'
--- includes/common.inc	2009-04-24 08:15:50 +0000
+++ includes/common.inc	2009-04-25 19:25:22 +0000
@@ -2283,7 +2283,7 @@ function drupal_load_stylesheet($file, $
  * @return
  *   Contents of the stylesheet including the imported stylesheets.
  */
-function drupal_load_stylesheet_content($contents, $optimize = FALSE) {  
+function drupal_load_stylesheet_content($contents, $optimize = FALSE) {
   // Replaces @import commands with the actual stylesheet content.
   // This happens recursively but omits external files.
   $contents = preg_replace_callback('/@import\s*(?:url\()?[\'"]?(?![a-z]+:)([^\'"\()]+)[\'"]?\)?;/', '_drupal_load_stylesheet', $contents);
@@ -3963,7 +3963,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'];
     }
 
@@ -3976,7 +3976,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-04-22 09:45:02 +0000
+++ modules/simpletest/tests/common.test	2009-04-25 19:23:14 +0000
@@ -866,15 +866,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;
@@ -960,4 +979,3 @@ class DrupalErrorCollectionUnitTest exte
     }
   }
 }
-

