{"record":{"id":"64ed8850deb5cd39","repo":"apache/shardingsphere","slug":"1050","errorCode":"1050","errorMessage":"Table '%s' already exists","messagePattern":"Table '(.+?)' already exists","errorType":"exception","errorClass":"TableExistsException","httpStatus":null,"severity":"error","filePath":"kernel/single/core/src/main/java/org/apache/shardingsphere/single/route/engine/SingleRouteEngine.java","lineNumber":118,"sourceCode":"            boolean containsIfNotExists = ((CreateTableStatement) sqlStatement).isIfNotExists();\n            if (dataNode.isPresent()) {\n                routeDDLStatementWithExistTable(routeContext, containsIfNotExists, dataNode.get(), table);\n            } else {\n                String dataSourceName = rule.assignNewDataSourceName();\n                routeContext.getRouteUnits().add(new RouteUnit(new RouteMapper(dataSourceName, dataSourceName), Collections.singleton(new RouteMapper(table.getTableName(), table.getTableName()))));\n            }\n        } else {\n            fillRouteContext(rule, routeContext, singleTables, database);\n        }\n    }\n    \n    private void routeDDLStatementWithExistTable(final RouteContext routeContext, final boolean containsIfNotExists, final DataNode dataNode, final QualifiedTable table) {\n        if (containsIfNotExists || hintValueContext.isSkipMetadataValidate()) {\n            String dataSourceName = dataNode.getDataSourceName();\n            routeContext.getRouteUnits()\n                    .add(new RouteUnit(new RouteMapper(dataSourceName, dataSourceName), Collections.singleton(new RouteMapper(table.getTableName(), table.getTableName()))));\n        } else {\n            throw new TableExistsException(table.getTableName());\n        }\n    }\n    \n    private void fillRouteContext(final SingleRule singleRule, final RouteContext routeContext, final Collection<QualifiedTable> logicTables, final ShardingSphereDatabase database) {\n        for (QualifiedTable each : logicTables) {\n            String tableName = each.getTableName();\n            DataNode dataNode = singleRule.findTableDataNode(database, each).orElseThrow(() -> new SingleTableNotFoundException(tableName));\n            String dataSource = dataNode.getDataSourceName();\n            routeContext.putRouteUnit(new RouteMapper(dataSource, dataSource), Collections.singletonList(new RouteMapper(tableName, tableName)));\n        }\n    }\n    \n    private void combineRouteContext(final RouteContext routeContext, final RouteContext newRouteContext) {\n        Map<String, RouteUnit> dataSourceRouteUnits = getDataSourceRouteUnits(newRouteContext);\n        routeContext.getRouteUnits().removeIf(each -> !dataSourceRouteUnits.containsKey(each.getDataSourceMapper().getLogicName()));\n        for (Entry<String, RouteUnit> entry : dataSourceRouteUnits.entrySet()) {\n            routeContext.putRouteUnit(entry.getValue().getDataSourceMapper(), entry.getValue().getTableMappers());\n        }","sourceCodeStart":100,"sourceCodeEnd":136,"githubUrl":"https://github.com/apache/shardingsphere/blob/e952770a215630a3659c75d64369168cd3e26b82/kernel/single/core/src/main/java/org/apache/shardingsphere/single/route/engine/SingleRouteEngine.java#L100-L136","documentation":"When a DDL statement routed by the single-rule engine targets a table that already exists, and the statement neither contains IF NOT EXISTS nor sets the skip-metadata-validation hint, SingleRouteEngine throws TableExistsException (error code 1050) with the table name. The engine refuses to route a CREATE TABLE that would collide with an existing single table.","triggerScenarios":"Executing CREATE TABLE t (...) through ShardingSphere when t is already a loaded single table in the target database; executing CREATE TABLE without IF NOT EXISTS while neither the SQL nor the hint context authorizes skipping validation.","commonSituations":"Re-running an init script that already executed; deploying schema migrations twice; legacy tables already present in the storage unit; ORACLE/MySQL dialect differences where IF NOT EXISTS is unsupported so users omit it.","solutions":["Add IF NOT EXISTS to the CREATE TABLE statement when the dialect supports it.","Drop or rename the existing table first if the intent is a clean re-create (DROP TABLE IF EXISTS t;).","For one-off schema sync where validation should be bypassed, set the SQL hint to skip metadata validation (/* SHARDINGSPHERE_HINT: ... */ per current hint syntax) — use sparingly.","Check SHOW SINGLE TABLES / metadata to see where the table is already registered."],"exampleFix":"-- before\nCREATE TABLE t_order (id INT, user_id INT);\n-- after\nCREATE TABLE IF NOT EXISTS t_order (id INT, user_id INT);","handlingStrategy":"validation","validationCode":"-- before creating, check existing single tables\nSHOW SINGLE TABLES;\n-- or test existence on storage\nSELECT COUNT(*) FROM information_schema.tables WHERE table_schema = DATABASE() AND table_name = 't_order';","typeGuard":null,"tryCatchPattern":"try (Statement st = conn.createStatement()) {\n    st.execute(\"CREATE TABLE t_order (...) /* IF NOT EXISTS variant */\");\n} catch (final TableExistsException ex) {\n    // treat as success when the table already has the expected schema\n}","preventionTips":["Always write IF NOT EXISTS in generated DDL for dialects that support it.","Make schema bootstrap scripts idempotent before running through ShardingSphere.","Inspect registered single tables before re-running init scripts."],"tags":["single","ddl","create-table","routing"],"backgroundTag":null,"analyzedSha":"e952770a215630a3659c75d64369168cd3e26b82","analyzedAt":"2026-08-14T13:54:53.392Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}