Fix PDOException "Base table or view not found" when deleting a field

Public

This error happens sometimes when deleting a field from an entity. The source of this issue is not clearly identified...

</> CopyGet raw version
sql
  1. /**
  2. * When deleting a field from any entity, if you encounter the error
  3. * PDOException : SQLSTATE[42S02]: Base table or view not found:
  4. * 1146 Table 'database.field_deleted_data_#id#'
  5. * where #id# is the field_id. Then do the following query :
  6. */
  7.  
  8. DELETE FROM field_config WHERE field_config.deleted = 1 AND field_config.id = #id#;
  9. DELETE FROM field_config_instance WHERE field_config_instance.field_id = #id#;

Comments

konrad's picture

The query is wrong in drupal use:
DELETE FROM {field_config} WHERE deleted = 1 AND id = 'id';
DELETE FROM {field_config_instance} WHERE field_id = 'id';

AlanT's picture

Well this snippet was intended to be executed directly from the DB side.
If you really want to go full Drupal (7) on this one :

  1. // Delete field instance
  2. db_delete('field_config_instance')
  3. ->condition('field_id', $field_id)
  4. ->execute();
  5.  
  6. // Delete the field.
  7. db_delete('field_config')
  8. ->condition('deleted', 1)
  9. ->condition('id', $field_name)
  10. ->execute();
Fab's picture

The query is right... It's SQL, there's no need here to express it the Drupal way.