{"record":{"id":"ff68c6b1c238eb19","repo":"MyCATApache/Mycat-Server","slug":"only-one-rule-can-defined-name","errorCode":null,"errorMessage":"only one rule can defined :${name}","messagePattern":"only one rule can defined :(.+?)","errorType":"exception","errorClass":"ConfigException","httpStatus":null,"severity":"error","filePath":"src/main/java/io/mycat/config/loader/xml/XMLRuleLoader.java","lineNumber":140,"sourceCode":"     */\r\n\tprivate void loadTableRules(Element root) throws SQLSyntaxErrorException {\r\n\t\t//获取每个tableRule标签\r\n\t\tNodeList list = root.getElementsByTagName(\"tableRule\");\r\n\t\tfor (int i = 0, n = list.getLength(); i < n; ++i) {\r\n\t\t\tNode node = list.item(i);\r\n\t\t\tif (node instanceof Element) {\r\n\t\t\t\tElement e = (Element) node;\r\n\t\t\t\t//先判断是否重复\r\n\t\t\t\tString name = e.getAttribute(\"name\");\r\n\t\t\t\tif (tableRules.containsKey(name)) {\r\n\t\t\t\t\tthrow new ConfigException(\"table rule \" + name\r\n\t\t\t\t\t\t\t+ \" duplicated!\");\r\n\t\t\t\t}\r\n\t\t\t\t//获取rule标签\r\n\t\t\t\tNodeList ruleNodes = e.getElementsByTagName(\"rule\");\r\n\t\t\t\tint length = ruleNodes.getLength();\r\n\t\t\t\tif (length > 1) {\r\n\t\t\t\t\tthrow new ConfigException(\"only one rule can defined :\"\r\n\t\t\t\t\t\t\t+ name);\r\n\t\t\t\t}\r\n\t\t\t\t//目前只处理第一个，未来可能有多列复合逻辑需求\r\n\t\t\t\t//RuleConfig是保存着rule与function对应关系的对象\r\n\t\t\t\tRuleConfig rule = loadRule((Element) ruleNodes.item(0));\r\n\t\t\t\tString funName = rule.getFunctionName();\r\n\t\t\t\t//判断function是否存在，获取function\r\n\t\t\t\tAbstractPartitionAlgorithm func = functions.get(funName);\r\n\t\t\t\tif (func == null) {\r\n\t\t\t\t\tthrow new ConfigException(\"can't find function of name :\"\r\n\t\t\t\t\t\t\t+ funName);\r\n\t\t\t\t}\r\n\t\t\t\trule.setRuleAlgorithm(func);\r\n\t\t\t\t//保存到tableRules\r\n\t\t\t\ttableRules.put(name, new TableRuleConfig(name, rule));\r\n\t\t\t}\r\n\t\t}\r\n\t}\r","sourceCodeStart":122,"sourceCodeEnd":158,"githubUrl":"https://github.com/MyCATApache/Mycat-Server/blob/65f8d8beb752f935752f2a0eec0ab017facab9ef/src/main/java/io/mycat/config/loader/xml/XMLRuleLoader.java#L122-L158","documentation":"Within one <tableRule> element, loadTableRules counts nested <rule> child elements. MyCat only supports a single rule (sharding column/algorithm pair) per tableRule, so more than one <rule> tag throws this ConfigException. The comment notes multi-column composite rules are planned but not implemented in this version.","triggerScenarios":"A rule.xml tableRule block containing two or more <rule> children, e.g. trying to declare sharding on two columns; load() -> loadTableRules() sees ruleNodes.getLength() > 1.","commonSituations":"Developers attempting composite/multi-column sharding by adding a second <rule> entry; copy-pasted example configs from newer MyCat forks that support multiple rules; merged XML fragments.","solutions":["Keep exactly one <rule> element per <tableRule> and restart.","If multi-column sharding is needed, use an algorithm that itself handles composite keys (e.g. a custom AbstractPartitionAlgorithm fed one concatenated column or a complex sharding function) instead of multiple <rule> tags.","Upgrade to a MyCat version that supports multi-column rules if the requirement is hard."],"exampleFix":"// before (rule.xml)\n<tableRule name=\"order-rule\">\n  <rule><columns>id</columns><algorithm>mod-long</algorithm></rule>\n  <rule><columns>cust_id</columns><algorithm>hash-int</algorithm></rule>\n</tableRule>\n// after\n<tableRule name=\"order-rule\">\n  <rule><columns>id</columns><algorithm>mod-long</algorithm></rule>\n</tableRule>","handlingStrategy":"validation","validationCode":"// Assert each tableRule has exactly one <rule> child before deployment\nNodeList tableRules = doc.getElementsByTagName(\"tableRule\");\nfor (int i = 0; i < tableRules.getLength(); i++) {\n    Element tr = (Element) tableRules.item(i);\n    if (tr.getElementsByTagName(\"rule\").getLength() != 1)\n        throw new IllegalStateException(\"tableRule \" + tr.getAttribute(\"name\") + \" must define exactly one <rule>\");\n}","typeGuard":null,"tryCatchPattern":"try {\n    ruleLoader.load();\n} catch (ConfigException e) {\n    if (e.getMessage().startsWith(\"only one rule can defined\")) {\n        LOG.error(\"Multi-rule tableRule not supported: {}\", e.getMessage());\n    }\n    throw e;\n}","preventionTips":["Document that this MyCat version supports one rule per tableRule.","Handle multi-column sharding inside the algorithm class, not via extra <rule> tags.","Validate rule.xml in CI before shipping configs."],"tags":["configuration","xml","unsupported-operation","sharding"],"backgroundTag":"unsupported-operation","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"}