Creating programatically a node that has a programtically created image field that should stay empty I'm dealing building several modules that should cope with automatic creation of content synchronized with external sources.
The whole process is automatized, e.g. everything had been created programatically.
My content type has and image field attached to it.
When appempting to create a node without defining what this image field should contain (and it should stay empty), I fall with exception:

    PDOException: SQLSTATE[23000]: Integrity constraint violation: 1048
    Column 'fid' cannot be null: INSERT INTO {file_usage} (fid, module, type, id, count) 
    VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3, :db_insert_placeholder_4);
    Array ( [:db_insert_placeholder_0] =&gt; [:db_insert_placeholder_1] =&gt; file [:db_insert_placeholder_2] =&gt; node [:db_insert_placeholder_3] =&gt; 264 [:db_insert_placeholder_4] =&gt; 1 ) 
    in file_usage_add() (line 657 of /usr/local/drupal7/includes/file.inc).


When on hook_node_presave I'm adding an image to this field all works correctly, but I would like to leave this field empty, that may affect the rest of development process.

If I create the node type programatically but add the image field manually -- all works ok. I tried it, then printed the definition of this field using function field_info_field() and copied some values of there into my code, but it didn't help.

Here is my image field definition:

...

'field_member_photo' =&gt; array(
'label' =&gt; t('Member Photo'),
'field_name' =&gt; 'field_member_photo',
'type' =&gt; 'image',
'module' =&gt; 'image',
'active' =&gt; '1',
'locked' =&gt; '0',
'cardinality' =&gt; '1',
'deleted' =&gt; '0',
'widget' =&gt; 'image_image',
'entity_types' =&gt; array('node'),
'settings' =&gt; array('uri_scheme' =&gt; 'public', 'default_image' =&gt; 0,),
),

...


Would appreciate any thought about how to solve this problem.
I think this is already been covered here some where but i'll answer this any way because creating fields manually is not something easy.

Let's start:
1. First thing - create the field through the UI
2. Export the fields with features. In the latest version of features, features devide the export to to diffrent things: file with all the fileds settings and a file with all the field instances. From the settings of the field that features create you need only the settings: cardinality, field name, module and settings. From the instance you will need the settings: label, required and the widget. You can save them to a function.


After you saved the settings you will need to use a couple of functions for next you will install your module or anything like this: field_info_field(), this function will let return you the settings of the field if he exists, if not you will need to create it with the function field_create_field() like this:

field_create_field($field_config);


The argument you'll need to give her is the settings of the field you saved later. For attaching the field to an entity you'll need to use the function field_create_instance that get the instance settings you save with three more keys: 


field_name - the name of the field
bundle - node type, message type etc. etc, 
entity_type - the entity type: node, comment, message etc. etc.


You can see here hot it's done:

// This is the instance settings you saved.
$instance;
$instance += array(
  'field_name' =&gt; $field_config['field_name'],
  'bundle' =&gt; 'article',
  'entity_type' =&gt; 'node',
);

field_create_instance($instance);


You can have a look in the module OG in the function og_create_field() and how he saved fields inside hook. This could very easy and other module you will wrote can be based on this if you have a very a lot of fields.