The file used in the {Drupal7} field may not be referenced.

After having upgraded my Drupal 7-alpha6 installation to Drupal 7.0, I started getting this error every time I tried to save any node or other entity (e.g. taxonomy term) that had an image field attached to it.

Some head-scratching and code-diving later, and I noticed that the way in which managed files were stored in the database had changed from my alpha6 code, and a new {file_usage} table had been introduced. Seeing as upgrades from alpha versions are not officially supported, I shouldn’t have been particularly surprised that my database didn’t contain this table – the appropriate system_update() had obviously not fired, but the error message could have been more helpful….

Fortunately, having identified the problem, the solution was thankfully simple.

1.) First, create the missing {file_usage} table using the following schema:

CREATE TABLE IF NOT EXISTS `file_usage` (

  `fid` int(10) unsigned NOT NULL COMMENT 'File ID.',

  `module` varchar(255) NOT NULL default '' COMMENT 'The name of the module that is using the file.',

  `type` varchar(64) NOT NULL default '' COMMENT 'The name of the object type in which the file is used.',

  `id` int(10) unsigned NOT NULL default '0' COMMENT 'The primary key of the object using the file.',

  `count` int(10) unsigned NOT NULL default '0' COMMENT 'The number of times this file is used by this object.',

  PRIMARY KEY  (`fid`,`type`,`id`,`module`),

  KEY `type_id` (`type`,`id`),

  KEY `fid_count` (`fid`,`count`),

  KEY `fid_module` (`fid`,`module`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Track where a file is used.';

2.) Then, populate the table with details of all the files that are being used anywhere in the site. For example, I had an image field defined by the field module that I used to attach images to nodes. So the data for this field was stored in the field_data_field_image table. To populate the {file_usage} table with details of the file sources of each of these images, I ran the following script:

INSERT INTO file_usage

SELECT

  field_image_fid AS fid,

  'file' AS module,

  'node' AS type,

  entity_id AS id,

  1 AS count

FROM

  field_data_field_image

I then repeated this for any other file reference or image fields throughout the site (replacing the ‘node’ type with ‘taxonomy_term’ for those images attached to taxonomy terms), and voila – the error went away and I could edit my entities with attached files again.

About these ads
This entry was posted in Drupal and tagged , , . Bookmark the permalink.

4 Responses to The file used in the {Drupal7} field may not be referenced.

  1. Thanks for this – I just had the same issue when upgrading my blog!

  2. Gedrick says:

    Can’t thank you enough for posting this solution! Worked great :)

  3. Massive thanks for sharing this! My images were under `field_data_field_logo` rather than `field_data_field_image` so I changed that in the query and everything worked. Here\’s the query I used:
    INSERT INTO file_usage SELECT field_logo_fid AS fid, \’file\’ AS module, \’node\’ AS type, entity_id AS id, 1 AS count FROM field_data_field_logo ;

    Again, thanks for posting this. Likely saved me an hour easy.

  4. At least in the preview, there’s some extra “\” escape characters. If my comment is approved and makes it to the comment section, ignore them. There was never a “\” in my query.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s