{"record":{"id":"d662fc640a49514e","repo":"MyCATApache/Mycat-Server","slug":"bad-insert-sql-sharding-column-not-provided","errorCode":null,"errorMessage":"bad insert sql (sharding column: not provided,","messagePattern":"bad insert sql \\(sharding column: not provided,","errorType":"exception","errorClass":"SQLNonTransientException","httpStatus":null,"severity":"error","filePath":"src/main/java/io/mycat/route/parser/druid/impl/DruidInsertParser.java","lineNumber":203,"sourceCode":"\t\t\tif(partitionColumn.equalsIgnoreCase(StringUtil.removeBackquote(insertStmt.getColumns().get(i).toString()))) {//找到分片字段\n\t\t\t\tisFound = true;\n\t\t\t\tString column = StringUtil.removeBackquote(insertStmt.getColumns().get(i).toString());\n\n\t\t\t\tString shardingValue = StringUtil.removeBackquote(getShardingValue(insertStmt.getValues().getValues().get(i)));\n\t\t\t\tinsertStmt.getValues().getValues().set(i,new SQLCharExpr(shardingValue));\n\t\t\t\tctx.setSql(insertStmt.toString());\n\n\t\t\t\tRouteCalculateUnit routeCalculateUnit = new RouteCalculateUnit();\n\t\t\t\trouteCalculateUnit.addShardingExpr(tableName, column, shardingValue);\n\t\t\t\tctx.addRouteCalculateUnit(routeCalculateUnit);\n\t\t\t\t//mycat是单分片键，找到了就返回\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tif(!isFound) {//分片表的\n\t\t\tString msg = \"bad insert sql (sharding column:\"+ partitionColumn + \" not provided,\" + insertStmt;\n\t\t\tLOGGER.warn(msg);\n\t\t\tthrow new SQLNonTransientException(msg);\n\t\t}\n\t\t// insert into .... on duplicateKey \n\t\t//such as :INSERT INTO TABLEName (a,b,c) VALUES (1,2,3) ON DUPLICATE KEY UPDATE b=VALUES(b); \n\t\t//INSERT INTO TABLEName (a,b,c) VALUES (1,2,3) ON DUPLICATE KEY UPDATE c=c+1;\n\t\tif(insertStmt.getDuplicateKeyUpdate() != null) {\n\t\t\tList<SQLExpr> updateList = insertStmt.getDuplicateKeyUpdate();\n\t\t\tfor(SQLExpr expr : updateList) {\n\t\t\t\tSQLBinaryOpExpr opExpr = (SQLBinaryOpExpr)expr;\n\t\t\t\tString column = StringUtil.removeBackquote(opExpr.getLeft().toString().toUpperCase());\n\t\t\t\tif(column.equals(partitionColumn)) {\n\t\t\t\t\tString msg = \"Sharding column can't be updated: \" + tableName + \" -> \" + partitionColumn;\n\t\t\t\t\tLOGGER.warn(msg);\n\t\t\t\t\tthrow new SQLNonTransientException(msg);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\t","sourceCodeStart":185,"sourceCodeEnd":221,"githubUrl":"https://github.com/MyCATApache/Mycat-Server/blob/65f8d8beb752f935752f2a0eec0ab017facab9ef/src/main/java/io/mycat/route/parser/druid/impl/DruidInsertParser.java#L185-L221","documentation":"In parserSingleInsert, Mycat scans the INSERT column list for the table's partitionColumn. If the sharding column is not among the columns (isFound stays false), the target data node cannot be computed, so a SQLNonTransientException 'bad insert sql (sharding column:X not provided' is thrown with the statement appended.","triggerScenarios":"Single-row INSERT into a sharded table whose explicit column list omits the configured partitionColumn, e.g. INSERT INTO orders (id, name) VALUES (...) where partitionColumn is customer_id.","commonSituations":"Column lists that predate a partitionColumn being added to config; ORM partial inserts; case mismatch between the SQL column name and the uppercase partitionColumn in schema.xml.","solutions":["Add the sharding column and its value to the INSERT column list and VALUES.","Check that partitionColumn in schema.xml matches the actual column name (compared uppercase, backquotes removed).","If the column truly has no value, make it nullable-with-default and choose a different sharding column, or change the rule."],"exampleFix":"// before (partitionColumn = customer_id)\nINSERT INTO orders (id, name) VALUES (1, 'x');\n// after\nINSERT INTO orders (id, name, customer_id) VALUES (1, 'x', 100);","handlingStrategy":"validation","validationCode":"// Ensure the sharding column is in the column list before issuing the insert\nfunction validateShardingColumn(columns, partitionColumn) {\n  if (!columns.map(c => c.replace(/[`\\\"]/g, '').toUpperCase()).includes(partitionColumn.toUpperCase())) {\n    throw new Error(\"INSERT must include sharding column: \" + partitionColumn);\n  }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always include the partitionColumn in INSERT column lists for sharded tables.","Re-check application SQL after changing partitionColumn in schema.xml."],"tags":["insert","sharding-column","missing-column"],"backgroundTag":"missing-required-argument","analyzedSha":"65f8d8beb752f935752f2a0eec0ab017facab9ef","analyzedAt":"2026-09-11T00:12:21.696Z","contentChangedAt":"2026-09-11T00:12:21.696Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}