{"record":{"id":"4e074d3bc7493288","repo":"getredash/redash","slug":"error-creating-table","errorCode":null,"errorMessage":"Error creating table {}: {}","messagePattern":"Error creating table (.+?): (.+?)","errorType":"exception","errorClass":"CreateTableError","httpStatus":null,"severity":"error","filePath":"redash/query_runner/query_results.py","lineNumber":132,"sourceCode":"    elif isinstance(value, datetime.timedelta):\n        return str(value)\n    else:\n        return value\n\n\ndef create_table(connection, table_name, query_results):\n    try:\n        columns = [column[\"name\"] for column in query_results[\"columns\"]]\n        safe_columns = [fix_column_name(column) for column in columns]\n\n        column_list = \", \".join(safe_columns)\n        create_table = \"CREATE TABLE {table_name} ({column_list})\".format(\n            table_name=table_name, column_list=column_list\n        )\n        logger.debug(\"CREATE TABLE query: %s\", create_table)\n        connection.execute(create_table)\n    except sqlite3.OperationalError as exc:\n        raise CreateTableError(\"Error creating table {}: {}\".format(table_name, str(exc)))\n\n    insert_template = \"insert into {table_name} ({column_list}) values ({place_holders})\".format(\n        table_name=table_name,\n        column_list=column_list,\n        place_holders=\",\".join([\"?\"] * len(columns)),\n    )\n\n    for row in query_results[\"rows\"]:\n        values = [flatten(row.get(column)) for column in columns]\n        connection.execute(insert_template, values)\n\n\ndef prepare_parameterized_query(query, query_params):\n    for params in query_params:\n        table_hash = hashlib.md5(\n            \"query_{query}_{hash}\".format(query=params[0], hash=params[1]).encode(), usedforsecurity=False\n        ).hexdigest()\n        key = \"param_query_{query_id}_{{{param_string}}}\".format(query_id=params[0], param_string=params[1])","sourceCodeStart":114,"sourceCodeEnd":150,"githubUrl":"https://github.com/getredash/redash/blob/ca79fe988d81cdac9675b412f3dfcab107bc1fbc/redash/query_runner/query_results.py#L114-L150","documentation":"Raised by create_table (redash/query_runner/query_results.py:132) as CreateTableError when connection.execute of the generated 'CREATE TABLE ...' statement raises sqlite3.OperationalError. The message embeds the table name and the sqlite error text, which names the actual cause (duplicate table, illegal identifier, etc.). This code backs the query_results loader that materializes other queries' results into a temp SQLite database.","triggerScenarios":"Calling create_tables_from_query_ids / create_table twice for the same table name (table already exists), or with column names containing characters SQLite rejects unquoted (e.g. mismatched quoting when double quotes or colons are involved), or when the column list is empty producing invalid SQL.","commonSituations":"Rerunning a results-loader query against a persistent connection where the temp table was not dropped; upstream result sets whose column names contain quotes/colons that break identifier quoting; schema drift producing columns the quoting logic mishandles.","solutions":["Read the embedded sqlite error text — 'table X already exists' means DROP TABLE IF EXISTS first or use a fresh temp name","Sanitize/quote column names before create_table (replace '\"' and unusual characters)","For loader queries, ensure each run uses a new in-memory SQLite connection or drops previously created tables"],"exampleFix":"# before\ncreate_table(connection, table_name, columns)\n# after\nconnection.execute('DROP TABLE IF EXISTS {}'.format(table_name))\ncreate_table(connection, table_name, columns)","handlingStrategy":"try-catch","validationCode":"existing = connection.execute(\"select name from sqlite_master where type='table' and name=?\", (table_name,)).fetchone()\nif existing:\n    connection.execute('DROP TABLE IF EXISTS {}'.format(table_name))","typeGuard":null,"tryCatchPattern":"try:\n    create_table(connection, table_name, columns)\nexcept CreateTableError as e:\n    logger.error('create_table failed: %s', e)\n    connection.execute('DROP TABLE IF EXISTS {}'.format(table_name))\n    create_table(connection, table_name, columns)","preventionTips":["Always create loader tables in a fresh in-memory SQLite connection per run","DROP TABLE IF EXISTS before recreating in persistent connections","Sanitize column names (strip quotes/colons) before DDL generation"],"tags":["sqlite","redash","ddl","operational-error"],"backgroundTag":"sqlite-create-table-failed","analyzedSha":"ca79fe988d81cdac9675b412f3dfcab107bc1fbc","analyzedAt":"2026-08-28T18:32:34.637Z","schemaVersion":2},"datasetVersion":"2026-08-28T21:17:43.275Z"}