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.