Integrity constraint violation: 1052 Column 'type' in where clause is ambiguous

Created on 11 April 2023, over 1 year ago
Updated 4 June 2024, 6 months ago

Problem/Motivation

Adding a join to a query inside "hook_query_TAG_alter" (hook_query_views_block_content_alter) on a table with field "type" results in the error "Integrity Constraint Violation: 1052 Column 'type' in clause where is ambiguous."

This error only appears for users without the "view restricted block content" permission.

Steps to reproduce

1. Add join query in the 'hook_query_views_block_content_alter' to the 'block_content' table, i.e.,

$query->join('block_content', 'bc', 'block_content_field_data.id = bc.id')

or add filter to the 'block_content' view by 'uuid' field

2. Visit page with blocks listing

Result:

The website encountered an unexpected error. Please try again later.

Drupal\Core\Database\IntegrityConstraintViolationException: SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'type' in where clause is ambiguous

Proposed resolution

This error is caused by an existing field name "type" in the joined table that also exists in the "block_content_field_data" table.

I managed to fix this error by adding 'block_content_field_data' table alias to the 'where' conditions added in the 'block_content_permissions_views_query_alter' hook:

      // Restrict query to allowed block content types.
      if (!empty($allowedBlockContentTypes)) {
        $query->addWhere('1', 'block_content_permissions_views_query_alter.type', $allowedBlockContentTypes, 'IN');
      }
      else {
        $query->addWhere('1', 'block_content_permissions_views_query_alter.type', '', '=');
      }
✨ Feature request
Status

RTBC

Version

1.0

Component

Code

Created by

πŸ‡ΊπŸ‡¦Ukraine ihor_allin Kyiv

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @ihor_allin
  • πŸ‡ΊπŸ‡¦Ukraine ihor_allin Kyiv
  • πŸ‡ΊπŸ‡¦Ukraine ihor_allin Kyiv
  • πŸ‡ΊπŸ‡¦Ukraine ihor_allin Kyiv
  • πŸ‡ΊπŸ‡¦Ukraine ihor_allin Kyiv
  • πŸ‡ΊπŸ‡ΈUnited States joshua.roberson

    I tested the module without modification on D9.5.3 and D10.0.3 with a user without the "view restricted block content" permission. The page works correctly and nothing was reported in the log messages. Seems like you want to extend the current query to something more by adding the "join", so this isn't a bug. Can you elaborate on what you are trying to accomplish?

  • Status changed to Postponed: needs info over 1 year ago
  • Status changed to Closed: works as designed over 1 year ago
  • πŸ‡§πŸ‡ͺBelgium JeroenT πŸ‡§πŸ‡ͺ

    I can reproduce this error. By e.g. enabling the group β†’ module.

    The error I receive is the following:
    Drupal\Core\Database\IntegrityConstraintViolationException: SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'type' in where clause is ambiguous: SELECT "block_content_field_data"."langcode" AS "block_content_field_data_langcode", "block_content_field_data"."id" AS "id" FROM "block_content_field_data" "block_content_field_data" LEFT OUTER JOIN "group_relationship_field_data" "gcfd" ON block_content_field_data.id=gcfd.entity_id AND gcfd.plugin_id IN (:plugin_ids_in_use__0) LEFT OUTER JOIN "group_relationship_field_data" "gcfd_2" ON gcfd.gid=gcfd_2.gid AND gcfd_2.plugin_id='group_membership' AND gcfd_2.entity_id=:account_id WHERE ("type" IN (:db_condition_placeholder_0, :db_condition_placeholder_1, :db_condition_placeholder_2, :db_condition_placeholder_3, :db_condition_placeholder_4, :db_condition_placeholder_5, :db_condition_placeholder_6, :db_condition_placeholder_7, :db_condition_placeholder_8, :db_condition_placeholder_9, :db_condition_placeholder_10, :db_condition_placeholder_11, :db_condition_placeholder_12, :db_condition_placeholder_13, :db_condition_placeholder_14, :db_condition_placeholder_15, :db_condition_placeholder_16, :db_condition_placeholder_17, :db_condition_placeholder_18, :db_condition_placeholder_19, :db_condition_placeholder_20, :db_condition_placeholder_21, :db_condition_placeholder_22, :db_condition_placeholder_23)) AND (("gcfd"."entity_id" IS NULL) OR (("block_content_field_data"."status" = :db_condition_placeholder_24) AND (("gcfd"."gid" IN (:db_condition_placeholder_25, :db_condition_placeholder_26, :db_condition_placeholder_27, :db_condition_placeholder_28, :db_condition_placeholder_29, :db_condition_placeholder_30, :db_condition_placeholder_31, :db_condition_placeholder_32, :db_condition_placeholder_33, :db_condition_placeholder_34)) AND ("gcfd"."plugin_id" = :db_condition_placeholder_35))) OR (("block_content_field_data"."status" = :db_condition_placeholder_36) AND ((("gcfd"."type" IN (:db_condition_placeholder_37)) AND ("gcfd_2"."entity_id" IS NULL)) OR (("gcfd"."type" IN (:db_condition_placeholder_38)) AND ("gcfd_2"."entity_id" IS NOT NULL)) OR (("gcfd"."gid" IN (:db_condition_placeholder_39, :db_condition_placeholder_40, :db_condition_placeholder_41, :db_condition_placeholder_42, :db_condition_placeholder_43, :db_condition_placeholder_44, :db_condition_placeholder_45, :db_condition_placeholder_46, :db_condition_placeholder_47, :db_condition_placeholder_48, :db_condition_placeholder_49)) AND ("gcfd"."plugin_id" = :db_condition_placeholder_50))))) ORDER BY "block_content_field_data"."changed" DESC LIMIT 51 OFFSET 0; Array ( [:db_condition_placeholder_0] => basic [:db_condition_placeholder_1] => campaign_countdown [:db_condition_placeholder_2] => content_accordeon [:db_condition_placeholder_3] => content_banner_reference [:db_condition_placeholder_4] => content_carousel [:db_condition_placeholder_5] => content_contact_our_associate [:db_condition_placeholder_6] => content_cta [:db_condition_placeholder_7] => content_cta_container [:db_condition_placeholder_8] => content_download [:db_condition_placeholder_9] => content_focus [:db_condition_placeholder_10] => content_follow_us [:db_condition_placeholder_11] => content_form_info [:db_condition_placeholder_12] => content_gallery [:db_condition_placeholder_13] => content_image [:db_condition_placeholder_14] => content_image_matrix [:db_condition_placeholder_15] => content_infographic [:db_condition_placeholder_16] => content_media_text [:db_condition_placeholder_17] => content_quote [:db_condition_placeholder_18] => content_referenced_content [:db_condition_placeholder_19] => content_support [:db_condition_placeholder_20] => content_text [:db_condition_placeholder_21] => content_timeline [:db_condition_placeholder_22] => content_video [:db_condition_placeholder_23] => sidebar_banner [:db_condition_placeholder_24] => 0 [:db_condition_placeholder_25] => 1 [:db_condition_placeholder_26] => 2 [:db_condition_placeholder_27] => 3 [:db_condition_placeholder_28] => 5 [:db_condition_placeholder_29] => 6 [:db_condition_placeholder_30] => 7 [:db_condition_placeholder_31] => 10 [:db_condition_placeholder_32] => 13 [:db_condition_placeholder_33] => 15 [:db_condition_placeholder_34] => 16 [:db_condition_placeholder_35] => group_block:content_text [:db_condition_placeholder_36] => 1 [:db_condition_placeholder_37] => pillar-group_block-content_text [:db_condition_placeholder_38] => pillar-group_block-content_text [:db_condition_placeholder_39] => 1 [:db_condition_placeholder_40] => 2 [:db_condition_placeholder_41] => 3 [:db_condition_placeholder_42] => 5 [:db_condition_placeholder_43] => 6 [:db_condition_placeholder_44] => 7 [:db_condition_placeholder_45] => 10 [:db_condition_placeholder_46] => 13 [:db_condition_placeholder_47] => 8 [:db_condition_placeholder_48] => 15 [:db_condition_placeholder_49] => 16 [:db_condition_placeholder_50] => group_block:content_text [:account_id] => 4386 [:plugin_ids_in_use__0] => group_block:content_text ) in Drupal\mysql\Driver\Database\mysql\ExceptionHandler->handleExecutionException() (regel 43 van /var/www/11.be/releases/20240530065340-master-2418223/htdocs/web/core/modules/mysql/src/Driver/Database/mysql/ExceptionHandler.php).

  • Status changed to RTBC 6 months ago
  • πŸ‡§πŸ‡ͺBelgium JeroenT πŸ‡§πŸ‡ͺ

    The patch in #2 fixed the problem for me.

Production build 0.71.5 2024