I've just updated a site from a really old version to alpha2 and the following error shows for anonymous users:

Exception: Query tagged for file access but there is no fid. Add foreign keys to file_managed.fid in schema to fix. in _file_entity_query_file_entity_access_alter() (line 1812 of file_entity/file_entity.module).

This doesn't show for admin users.

Comments

Dave Reid’s picture

Status: Active » Postponed (maintainer needs more info)

Will probably need your help with reporting what the actual SQL query is that is causing this.

DamienMcKenna’s picture

Status: Postponed (maintainer needs more info) » Active

This happens if, for some reason, the foreign key is not added to the file_metadata table.

DamienMcKenna’s picture

In my scenario the error was coming from a view that had the "View published content" permission.

DamienMcKenna’s picture

FYi the problem may stem from #2064305: DB error on fresh install of module using sqlite where the primary key was changed. I noticed that after running through all of the database updates for alpha2, the table structure was left with a primary key but no foreign keys.

DamienMcKenna’s picture

This query should add the necessary foreign key:

ALTER TABLE file_metadata ADD CONSTRAINT file_managed FOREIGN KEY (fid) REFERENCES file_managed (fid);

DamienMcKenna’s picture

For my own notes, it was the gallery_content view, block_5 display.

DamienMcKenna’s picture

Title: Error for anonymous users after updating to alpha2 » Foreign key missing on {file_metadata} table after updating to alpha2

Just to note, I was looking at the wrong table, the primary keys are correct on the {file_metadata} table.

DamienMcKenna’s picture

FYI I'm resolving the issue on this one site by granting anon users and auth users the 'bypass file access' permission as we're not using file permissions at all, though obviously that's far from an ideal scenario.

DamienMcKenna’s picture

Further info - I added the foreign key to the table but the error still occurs.

Dave Reid’s picture

I was going to say the file_metadata table likely wouldn't be the problem in the query - which is why getting the actual query would be helpful.

DamienMcKenna’s picture

Here's the query I get from the Views admin:

SELECT node_field_data_field_gallery_items.nid AS node_field_data_field_gallery_items_nid, field_data_field_gallery_items.delta AS field_data_field_gallery_items_delta, 'node' AS field_data_field_gallery_item_media_node_entity_type
FROM 
{node} node
LEFT JOIN {field_data_field_gallery_items} field_data_field_gallery_items ON node.nid = field_data_field_gallery_items.entity_id AND (field_data_field_gallery_items.entity_type = 'node' AND field_data_field_gallery_items.deleted = '0')
LEFT JOIN {node} node_field_data_field_gallery_items ON field_data_field_gallery_items.field_gallery_items_nid = node_field_data_field_gallery_items.nid
LEFT JOIN {file_usage} node_field_data_field_gallery_items__file_usage ON node_field_data_field_gallery_items.nid = node_field_data_field_gallery_items__file_usage.id AND node_field_data_field_gallery_items__file_usage.type = 'node'
INNER JOIN {file_managed} file_managed_file_usage ON node_field_data_field_gallery_items__file_usage.fid = file_managed_file_usage.fid
WHERE (( (node.status = '1') AND (node.type IN  ('gallery')) AND (node.nid = '8436' ) ))
ORDER BY field_data_field_gallery_items_delta ASC
LIMIT 6 OFFSET 0
DamienMcKenna’s picture

Title: Foreign key missing on {file_metadata} table after updating to alpha2 » Error for anonymous users after updating to alpha2

Putting this back to the original bug as I opened another issue for the foreign key problem: #2069099: Foreign key missing on {file_metadata} table after updating to alpha2

Abhinesh Sharma’s picture

Hi All,

It's File Entity version problem. If we are using File Entity 7.x-2.0-alpha2 with Media Module version 7.x-2.0-alpha2 . After install both module it's throw same error.

The Main Region is file Entity module some Time don't create file_metadata filed in Database. After you want to update the file entity version but you can't because file entity module don't provide uninstall option in back-end.

So If you have found any error like PDO table{file_managed} or Table {file_metadata} then Check Database table Structure.

Search 2 Tables in Database are exist or not

1) file_managed
2)file_metadata

and Please verify In file_managed table field "type" is exist or not. if not then create Field

type varchar(50) utf8_general_ci No undefined

And for file_metadata Please Run Below Query in Sql:-

--
-- Table structure for table `file_metadata`
--

CREATE TABLE IF NOT EXISTS `file_metadata` (
`fid` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'The file_managed.fid of the metadata.',
`name` varchar(255) NOT NULL COMMENT 'The name of the metadata (e.g. ’width’).',
`value` longblob COMMENT 'The value of the metadata (e.g. ’200px’).',
PRIMARY KEY (`fid`,`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Cache images dimensions.';

May this Post will help for use any version of File Entity and Media Module.

Thanks
Abhinesh