Programmatically attaching files I have created the "Gallery" content type, and added two fields: "photo," and "document." I then used the following code for uploading a file in the "document" field:

$file = file_save_upload('document', array(
    'file_validate_extensions' =&gt; array('txt doc'), // Validate extensions.
));

// If the file passed validation:
if ($file) {
// Move the file, into the Drupal file system
if ($file = file_move($file, 'public://')) {
  $file-&gt;status = FILE_STATUS_PERMANENT;
 // $file-&gt;file_display = 1;
  $file = file_save($file);

} else {
  $output = t('Failed to write the uploaded file the site\'s file folder.');
}       
 } else {
$output = t('No file was uploaded.');
 }


I am attaching this file to the node using the following code:

$customNode-&gt;field_document[$customNode-&gt;language][0] = (array)$file;


When I call node_submit() function, I get the following error:


  Integrity constraint violation: 1048 Column 'field_document_display' cannot be null 


Does anybody know what I am doing wrong?
I usually don't throw the (array)$file line because really the only thing the field data needs is the fid, description and display. So I usually do the following:

$node-&gt;field_image[LANGUAGE_NONE][] = array(
  'fid' =&gt; $file-&gt;fid,
  'display' =&gt; 1,
  'description' =&gt; '',
);
node_save( $node );


This way if the display is required, I don't get an error. But that's just me...