{"record":{"id":"cfa5e201fdbdc6d4","repo":"run-llama/llama_index","slug":"table-name-must-be-specified","errorCode":null,"errorMessage":"table_name must be specified","messagePattern":"table_name must be specified","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"llama-index-core/llama_index/core/indices/common/struct_store/sql.py","lineNumber":34,"sourceCode":"class SQLStructDatapointExtractor(BaseStructDatapointExtractor):\n    \"\"\"Extracts datapoints from a structured document for a SQL db.\"\"\"\n\n    def __init__(\n        self,\n        llm: LLM,\n        schema_extract_prompt: BasePromptTemplate,\n        output_parser: OUTPUT_PARSER_TYPE,\n        sql_database: SQLDatabase,\n        table_name: Optional[str] = None,\n        table: Optional[Table] = None,\n        ref_doc_id_column: Optional[str] = None,\n    ) -> None:\n        \"\"\"Initialize params.\"\"\"\n        super().__init__(llm, schema_extract_prompt, output_parser)\n        self._sql_database = sql_database\n        # currently the user must specify a table info\n        if table_name is None and table is None:\n            raise ValueError(\"table_name must be specified\")\n        self._table_name = table_name or cast(Table, table).name\n        if table is None:\n            table_name = cast(str, table_name)\n            table = self._sql_database.metadata_obj.tables[table_name]\n        # if ref_doc_id_column is specified, then we need to check that\n        # it is a valid column in the table\n        col_names = [c.name for c in table.c]\n        if ref_doc_id_column is not None and ref_doc_id_column not in col_names:\n            raise ValueError(\n                f\"ref_doc_id_column {ref_doc_id_column} not in table {table_name}\"\n            )\n        self.ref_doc_id_column = ref_doc_id_column\n        # then store python types of each column\n        self._col_types_map: Dict[str, type] = {\n            c.name: table.c[c.name].type.python_type for c in table.c\n        }\n\n    def _get_col_types_map(self) -> Dict[str, type]:","sourceCodeStart":16,"sourceCodeEnd":52,"githubUrl":"https://github.com/run-llama/llama_index/blob/afd0fef371831f9bda13e5af7167cf4e981278ab/llama-index-core/llama_index/core/indices/common/struct_store/sql.py#L16-L52","documentation":"SQLTableContext must know which database table it extracts a schema for. It accepts either a table_name string or a SQLAlchemy Table object, and raises ValueError if both are None — there is no way to infer the target table. One of the two must identify the table.","triggerScenarios":"Constructing SQLTableContext(sql_database=db, table_name=None, table=None); building SQLStructIndex with a tables argument that fails to propagate a table name; calling the constructor with only keyword filters that don't map to table_name/table.","commonSituations":"Programmatically looping over tables and passing a falsy name by mistake; refactors where the positional order of (table_name, table) got swapped; relying on the index to auto-select a single table when several exist.","solutions":["Pass table_name='my_table' explicitly to SQLTableContext.","Alternatively pass the SQLAlchemy Table object directly: table=sql_db.metadata_obj.tables['my_table'].","If iterating tables, assert the name is non-empty before constructing the context."],"exampleFix":"# before\ncontext = SQLTableContext(\n    llm=llm, sql_database=sql_db,\n    table_name=None, table=None,  # ValueError\n)\n\n# after\ncontext = SQLTableContext(\n    llm=llm, sql_database=sql_db,\n    table_name=\"items\",\n)","handlingStrategy":"validation","validationCode":"if not table_name and table is None:\n    raise ValueError(\"Pass table_name or table for the target table\")","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Derive table_name from the same config/source that lists your tables so it is never None.","Assert the table exists in sql_database.metadata_obj.tables before building the context."],"tags":["sql","validation","constructor","table-context"],"backgroundTag":null,"analyzedSha":"afd0fef371831f9bda13e5af7167cf4e981278ab","analyzedAt":"2026-08-15T05:42:58.429Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}