{"record":{"id":"c70b39cd42b6af06","repo":"iflytek/astron-agent","slug":"error-creating-table-table","errorCode":null,"errorMessage":"Error creating table {table}","messagePattern":"Error creating table (.+?)","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"core/plugin/link/domain/models/utils.py","lineNumber":223,"sourceCode":"        table_names = inspector.get_table_names()\n        current_tables = [\"tools_schema\"]\n\n        if table_names and all(table in table_names for table in current_tables):\n            logger.debug(\"Database and tables already exist\")\n            return\n\n        logger.debug(\"Creating database and tables\")\n\n        for table in SQLModel.metadata.sorted_tables:\n            try:\n                table.create(self.engine, checkfirst=True)\n            except OperationalError as oe:\n                logger.warning(\n                    f\"Table {table} already exists, skipping. Exception: {oe}\"\n                )\n            except Exception as exc:\n                logger.error(f\"Error creating table {table}: {exc}\")\n                raise RuntimeError(f\"Error creating table {table}\") from exc\n\n        # Now check if the required tables exist, if not, something went wrong.\n        inspector = inspect(self.engine)\n        table_names = inspector.get_table_names()\n        for table_name in current_tables:\n            if table_name not in table_names:\n                logger.error(\"Something went wrong creating the database and tables.\")\n                logger.error(\"Please check your database settings.\")\n                raise RuntimeError(\n                    \"Something went wrong creating the database and tables.\"\n                )\n\n        logger.debug(\"Database and tables created successfully\")\n\n\nclass RedisService:\n    \"\"\"Redis service for caching operations.\n","sourceCodeStart":205,"sourceCodeEnd":241,"githubUrl":"https://github.com/iflytek/astron-agent/blob/5e758547a83371a5a4b29dadf4ac03e8dd527635/core/plugin/link/domain/models/utils.py#L205-L241","documentation":"create_db_and_tables() iterates SQLModel.metadata.sorted_tables and calls table.create(). Any exception other than OperationalError (which is treated as 'table already exists') is logged and re-raised as RuntimeError. It wraps the original exception via `from exc`, so the underlying cause (syntax, connection, permission, driver error) is in the chain.","triggerScenarios":"SQLAlchemy raises non-OperationalError exceptions during DDL: SQLAlchemyError subclasses like ProgrammingError for bad column definitions, ArgumentError for malformed types, or driver-level IntegrityError; also failures connecting via a misconfigured engine that surface as non-OperationalError exceptions.","commonSituations":"Model changes producing invalid DDL for the target DB (e.g. unsupported column types); wrong dialect/driver in the connection URL; database user lacking CREATE TABLE privileges surfacing as PermissionError-style driver exceptions; corrupted SQLModel metadata after bad model edits.","solutions":["Read the chained cause (`The above exception was the direct cause of...`) in the traceback — it holds the real database error; fix that first.","Verify the DB user has CREATE privileges on the schema/database.","Check the engine connection URL/dialect is correct for your database (e.g. mysql+pymysql://...).","Fix invalid column/type definitions in the SQLModel classes that produce bad DDL."],"exampleFix":"# before\nexcept Exception as exc:\n    logger.error(f\"Error creating table {table}: {exc}\")\n    raise RuntimeError(f\"Error creating table {table}\") from exc\n# after\n# inspect the full chained traceback to find the real cause, e.g.:\ntry:\n    create_db_and_tables()\nexcept RuntimeError as e:\n    logger.exception(\"root cause\", exc_info=e.__cause__)","handlingStrategy":"try-catch","validationCode":"from sqlalchemy import inspect\ninsp = inspect(engine)\n# verify connectivity and privileges before DDL\nengine.connect().execute(sqlalchemy.text(\"SELECT 1\"))","typeGuard":null,"tryCatchPattern":"try:\n    service.create_db_and_tables()\nexcept RuntimeError as e:\n    logger.exception(\"table creation failed; root cause: %r\", e.__cause__)","preventionTips":["Always inspect e.__cause__ — the RuntimeError message hides the real DB error.","Grant the DB user CREATE privileges before first boot.","Validate the connection URL/dialect early with a smoke SELECT 1 at startup."],"tags":["database","sqlalchemy","ddl"],"backgroundTag":"database-write-failed","analyzedSha":"5e758547a83371a5a4b29dadf4ac03e8dd527635","analyzedAt":"2026-09-12T08:03:51.356Z","contentChangedAt":"2026-09-12T08:03:51.356Z","schemaVersion":2},"datasetVersion":"2026-09-19T12:17:13.211Z"}