{"record":{"id":"640887722656195f","repo":"MyCATApache/Mycat-Server","slug":"replacestatement-can-t-be-supported-use-insert-in","errorCode":null,"errorMessage":" ReplaceStatement can't be supported,use insert into ...on duplicate key update... instead ","messagePattern":" ReplaceStatement can't be supported,use insert into \\.\\.\\.on duplicate key update\\.\\.\\. instead ","errorType":"exception","errorClass":"SQLSyntaxErrorException","httpStatus":null,"severity":"error","filePath":"src/main/java/io/mycat/route/impl/DruidMycatRouteStrategy.java","lineNumber":605,"sourceCode":"\t/**\n\t * SELECT 语句\n\t */\n\tprivate boolean isSelect(SQLStatement statement) {\n\t\tif(statement instanceof SQLSelectStatement) {\n\t\t\treturn true;\n\t\t}\n\t\treturn false;\n\t}\n\n\t/**\n\t * 检验不支持的SQLStatement类型 ：不支持的类型直接抛SQLSyntaxErrorException异常\n\t * @param statement\n\t * @throws SQLSyntaxErrorException\n\t */\n\tprivate void checkUnSupportedStatement(SQLStatement statement) throws SQLSyntaxErrorException {\n\t\t//不支持replace语句\n\t\tif (statement instanceof SQLReplaceStatement) {\n\t\t\tthrow new SQLSyntaxErrorException(\" ReplaceStatement can't be supported,use insert into ...on duplicate key update... instead \");\n\t\t}\n\t}\n\n\t/**\n\t * 分析 SHOW SQL\n\t */\n\t@Override\n\tpublic RouteResultset analyseShowSQL(SchemaConfig schema,\n\t\t\tRouteResultset rrs, String stmt) throws SQLSyntaxErrorException {\n\n\t\tString[] fields = SchemaUtil.parseShowTable(stmt);\n        if (\"1\".equals(fields[0])) {// show tables\n\t\t\tString relSchema = fields[3];\n\t\t\tString tableName = fields[8];\n            //\n            if (relSchema != null && !relSchema.equalsIgnoreCase(schema.getName())) {\n                schema = MycatServer.getInstance().getConfig().getSchemas().get(relSchema);\n                if (schema == null) {","sourceCodeStart":587,"sourceCodeEnd":623,"githubUrl":"https://github.com/MyCATApache/Mycat-Server/blob/65f8d8beb752f935752f2a0eec0ab017facab9ef/src/main/java/io/mycat/route/impl/DruidMycatRouteStrategy.java#L587-L623","documentation":"Mycat's Druid router explicitly rejects REPLACE statements via checkUnSupportedStatement. REPLACE INTO performs delete+insert semantics that conflict with Mycat's sharding routing and replication handling, so users are told to use INSERT ... ON DUPLICATE KEY UPDATE instead.","triggerScenarios":"Executing 'REPLACE INTO table ...' (any SQLReplaceStatement, including REPLACE ... SELECT) through Mycat routing; the check runs in routeNormalSqlWithAST0 via checkUnSupportedStatement.","commonSituations":"Porting MySQL scripts that rely on REPLACE; ORMs or sync tools (e.g. data import jobs) generating REPLACE INTO; developers using REPLACE as an upsert idiom.","solutions":["Rewrite the statement as INSERT ... ON DUPLICATE KEY UPDATE with the desired column values.","Pre-check existence in the application and issue UPDATE or INSERT accordingly.","If REPLACE semantics are truly needed, execute directly against the backend MySQL, bypassing Mycat for that connection.","Wrap the upsert in a stored procedure on the backend and call it through Mycat if supported."],"exampleFix":"// before\nREPLACE INTO users (id, name) VALUES (1, 'alice');\n// after\nINSERT INTO users (id, name) VALUES (1, 'alice') ON DUPLICATE KEY UPDATE name = VALUES(name);","handlingStrategy":"validation","validationCode":"if (sql.trim().toLowerCase().startsWith(\"replace \")) {\n    throw new IllegalArgumentException(\"REPLACE not supported by Mycat; use INSERT ... ON DUPLICATE KEY UPDATE\");\n}","typeGuard":null,"tryCatchPattern":"try { rrs = route(sql); } catch (SQLSyntaxErrorException e) {\n    if (e.getMessage().contains(\"ReplaceStatement can't be supported\")) {\n        // convert to INSERT ... ON DUPLICATE KEY UPDATE and retry once\n    } else throw e;\n}","preventionTips":["Ban REPLACE INTO in code review/SQL lint rules for Mycat targets.","Use INSERT ... ON DUPLICATE KEY UPDATE as the standard upsert.","Configure ORMs/sync tools to emit upserts instead of REPLACE.","Route REPLACE-needing jobs directly to backend MySQL if unavoidable."],"tags":["sql","upsert","unsupported-statement"],"backgroundTag":"unsupported-operation","analyzedSha":"65f8d8beb752f935752f2a0eec0ab017facab9ef","analyzedAt":"2026-09-11T00:12:21.696Z","contentChangedAt":"2026-09-11T00:12:21.696Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}