{"record":{"id":"363fd427defe3d10","repo":"iflytek/astron-agent","slug":"something-went-wrong-creating-the-database-and-tables","errorCode":null,"errorMessage":"Something went wrong creating the database and tables.","messagePattern":"Something went wrong creating the database and tables\\.","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"critical","filePath":"core/plugin/link/domain/models/utils.py","lineNumber":232,"sourceCode":"        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\n    Provides Redis connection management, caching operations, and both\n    single Redis and Redis Cluster support.\n    \"\"\"\n\n    name = \"redis_service\"\n\n    def __init__(\n        self,\n        cluster_addr: str,","sourceCodeStart":214,"sourceCodeEnd":250,"githubUrl":"https://github.com/iflytek/astron-agent/blob/5e758547a83371a5a4b29dadf4ac03e8dd527635/core/plugin/link/domain/models/utils.py#L214-L250","documentation":"After attempting to create all tables, create_db_and_tables() re-inspects the database and verifies that every required table (hardcoded ['tools_schema']) exists. If a required table is still absent, it raises RuntimeError. This is a post-condition check: table creation silently failed or was skipped without raising.","triggerScenarios":"DDL ran against a different database/schema than the one inspected; OperationalError 'table already exists' was swallowed but the table is in another schema; a failure path left table.create() skipped; the inspector looks at the wrong engine/database (connection points to a different DB than where DDL executed).","commonSituations":"Wrong DATABASE_URL pointing at an empty database while tables were created elsewhere earlier; MySQL schema/search_path mismatch; table created by another deploy under a different name; checkfirst=True skipping creation because a same-named table exists in a different schema of the same server.","solutions":["Verify the engine's connection URL points to the database you actually expect (log engine.url).","Manually list tables (SHOW TABLES / \\dt) in the connected DB to see what exists and under which schema.","Check earlier logs for swallowed OperationalError warnings from table.create() — the 'already exists' path may have hidden a real failure.","Create the missing table manually or drop/recreate the database, then rerun create_db_and_tables()."],"exampleFix":"# before\ntable.create(self.engine, checkfirst=True)\n# after\ntable.create(self.engine, checkfirst=True)\nlogger.info(\"created %s in db %s\", table.name, self.engine.url.database)  # confirm DDL hits the intended DB","handlingStrategy":"validation","validationCode":"from sqlalchemy import inspect\nnames = inspect(engine).get_table_names()\nassert \"tools_schema\" in names, f\"tools_schema missing from {engine.url}; tables={names}\"","typeGuard":null,"tryCatchPattern":"try:\n    service.create_db_and_tables()\nexcept RuntimeError:\n    logger.error(\"required tables missing; check DB URL=%s\", service.engine.url)","preventionTips":["Log engine.url at startup to confirm which database DDL runs against.","Watch for earlier 'Table ... already exists, skipping' warnings — they can mask real failures.","Pin a single DATABASE_URL across services so DDL and verification hit the same DB/schema."],"tags":["database","sqlalchemy","schema"],"backgroundTag":"entity-not-found","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"}