{"record":{"id":"a6a1694d9eb4d1eb","repo":"apache/beam","slug":"database-operation-failed-e","errorCode":null,"errorMessage":"Database operation failed: {e}","messagePattern":"Database operation failed: (.+?)","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"sdks/python/apache_beam/transforms/enrichment_handlers/cloudsql.py","lineNumber":405,"sourceCode":"      transaction = connection.begin()\n      try:\n        if params:\n          result = connection.execute(text(query), params)\n        else:\n          result = connection.execute(text(query))\n        # Materialize results while transaction is active.\n        data: Union[list[dict[str, Any]], dict[str, Any]]\n        if is_batch:\n          data = [row._asdict() for row in result]\n        else:\n          result_row = result.first()\n          data = result_row._asdict() if result_row else {}\n        # Explicitly commit the transaction.\n        transaction.commit()\n        return data\n      except Exception as e:\n        transaction.rollback()\n        raise RuntimeError(f\"Database operation failed: {e}\") from e\n    except Exception as e:\n      raise Exception(\n          f'Could not execute the query. Please check if the query is properly '\n          f'formatted and the table exists. {e}') from e\n    finally:\n      if connection:\n        connection.close()\n\n  def _build_batch_query(\n      self, requests: list[beam.Row], batch_size: int) -> str:\n    \"\"\"Build batched query with unique parameter names for multiple requests.\n\n    This method extracts parameter placeholders from the where_clause_template\n    using regex and creates unique parameter names for each batch item. The\n    parameter names in the template can be any valid identifiers (e.g., :id,\n    :param_0, :user_name) and don't need to match field names exactly.\n\n    For batch queries, placeholders are replaced with unique names like","sourceCodeStart":387,"sourceCodeEnd":423,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/transforms/enrichment_handlers/cloudsql.py#L387-L423","documentation":"_execute_query wraps the actual database execution; when the query, transaction, or commit fails inside the inner try block it rolls back and re-raises as RuntimeError('Database operation failed: ...') chaining the original DBAPI exception.","triggerScenarios":"Any DBAPI exception during cursor execution or commit: bad SQL syntax, missing table/column, constraint violations, dropped connections mid-query, wrong parameter binding types.","commonSituations":"Malformed where_clause_template producing invalid SQL; table renamed in the database; transient Cloud SQL connection resets; values violating NOT NULL or unique constraints.","solutions":["Read the chained exception (e.__cause__) to see the underlying DB error.","Verify the SQL and parameter names against the actual schema.","Check network/service-account connectivity to the Cloud SQL instance.","Retry transient connection errors; fix the query text for deterministic errors."],"exampleFix":"# before: RuntimeError: Database operation failed: column \"usr_id\" does not exist\nTableFieldsQueryConfig(table_name=\"users\", where_clause_template=\"usr_id = {id}\", ...)\n# after\nTableFieldsQueryConfig(table_name=\"users\", where_clause_template=\"id = {id}\", ...)","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":["python","database","sql","cloudsql","runtime-error"],"backgroundTag":"database-query-failed","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-20T03:17:13.778Z"}