? docs
? tmp
? modules/syndication
? sites/all/modules/admin_menu
? sites/all/modules/cck
? sites/all/modules/coder
? sites/all/modules/cvs_deploy
? sites/all/modules/demo
? sites/all/modules/devel
? sites/all/modules/drush
? sites/all/modules/google_analytics
? sites/all/modules/path_redirect
? sites/all/modules/pbs
? sites/all/modules/plugin_manager
? sites/all/modules/robotstxt
? sites/all/modules/sandbox
Index: includes/common.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/common.inc,v
retrieving revision 1.871
diff -u -p -r1.871 common.inc
--- includes/common.inc	17 Mar 2009 22:35:07 -0000	1.871
+++ includes/common.inc	21 Mar 2009 01:20:11 -0000
@@ -3904,6 +3904,7 @@ function drupal_write_record($table, &$o
   }
 
   $fields = array();
+  $object_fields = get_object_vars($object);
 
   // Go through our schema, build SQL, and when inserting, fill in defaults for
   // fields that are not set.
@@ -3914,7 +3915,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 (!isset($object->$field) && empty($primary_keys) && isset($info['default']) && !array_key_exists($field, $object_fields)) {
       $object->$field = $info['default'];
     }
 
@@ -3927,7 +3928,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) || (array_key_exists($field, $object_fields) && $info['not null'] == FALSE)) {
       if (empty($info['serialize'])) {
         $fields[$field] = $object->$field;
       }
Index: modules/simpletest/tests/common.test
===================================================================
RCS file: /cvs/drupal/drupal/modules/simpletest/tests/common.test,v
retrieving revision 1.30
diff -u -p -r1.30 common.test
--- modules/simpletest/tests/common.test	28 Feb 2009 07:36:06 -0000	1.30
+++ modules/simpletest/tests/common.test	21 Mar 2009 01:20:13 -0000
@@ -824,15 +824,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;
@@ -918,4 +937,3 @@ class DrupalErrorCollectionUnitTest exte
     }
   }
 }
-
