{"record":{"id":"9b43b206f2c616a4","repo":"MyCATApache/Mycat-Server","slug":"please-make-sure-the-primarykey-s-config-is-not-nu","errorCode":null,"errorMessage":"please make sure the primaryKey's config is not null in schemal.xml","messagePattern":"please make sure the primaryKey's config is not null in schemal\\.xml","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/main/java/io/mycat/route/util/RouterUtil.java","lineNumber":610,"sourceCode":"\t\t\tString origSQL, ServerConnection sc) throws SQLNonTransientException {\r\n\t\tString tableName = StringUtil.getTableName(origSQL).toUpperCase();\r\n\t\tTableConfig tableConfig = schema.getTables().get(tableName);\r\n\t\tboolean processedInsert=false;\r\n\t\t//判断是有自增字段\r\n\t\tif (null != tableConfig && tableConfig.isAutoIncrement()) {\r\n\t\t\tString primaryKey = tableConfig.getPrimaryKey();\r\n\t\t\tprocessedInsert=processInsert(sc,schema,sqlType,origSQL,tableName,primaryKey);\r\n\t\t}\r\n\t\treturn processedInsert;\r\n\t}\r\n\t/*\r\n\t *  找到返回主键的的位置\r\n\t *  找不到返回 -1\r\n\t * */\r\n\tprivate static int isPKInFields(String origSQL,String primaryKey,int firstLeftBracketIndex,int firstRightBracketIndex){\r\n\r\n\t\tif (primaryKey == null) {\r\n\t\t\tthrow new RuntimeException(\"please make sure the primaryKey's config is not null in schemal.xml\");\r\n\t\t}\r\n\r\n\t\tboolean isPrimaryKeyInFields = false;\r\n\t\tint  pkStart = 0;\r\n\t\tString upperSQL = origSQL.substring(firstLeftBracketIndex, firstRightBracketIndex + 1).toUpperCase();\r\n\t\tfor (int pkOffset = 0, primaryKeyLength = primaryKey.length();;) {\r\n\t\t\tpkStart = upperSQL.indexOf(primaryKey, pkOffset);\r\n\t\t\tif (pkStart >= 0 && pkStart < firstRightBracketIndex) {\r\n\t\t\t\tchar pkSide = upperSQL.charAt(pkStart - 1);\r\n\t\t\t\tif (pkSide <= ' ' || pkSide == '`' || pkSide == ',' || pkSide == '(') {\r\n\t\t\t\t\tpkSide = upperSQL.charAt(pkStart + primaryKey.length());\r\n\t\t\t\t\tisPrimaryKeyInFields = pkSide <= ' ' || pkSide == '`' || pkSide == ',' || pkSide == ')';\r\n\t\t\t\t}\r\n\t\t\t\tif (isPrimaryKeyInFields) {\r\n\t\t\t\t\tbreak;\r\n\t\t\t\t}\r\n\t\t\t\tpkOffset = pkStart + primaryKeyLength;\r\n\t\t\t} else {\r","sourceCodeStart":592,"sourceCodeEnd":628,"githubUrl":"https://github.com/MyCATApache/Mycat-Server/blob/65f8d8beb752f935752f2a0eec0ab017facab9ef/src/main/java/io/mycat/route/util/RouterUtil.java#L592-L628","documentation":"isPKInFields parses an INSERT's column list to locate the primary key's position, but the primaryKey argument itself is null because the table's configuration lacks a primaryKey. The method throws RuntimeException immediately since primary-key-based routing cannot proceed without it. Note the message says 'schemal.xml' but it refers to schema.xml.","triggerScenarios":"Executing an INSERT against a sharded table that uses the primary key for routing (or child-table join-key logic) while the <table> entry in schema.xml has no primaryKey attribute, and code path routeToMultiNode...insert calls isPKInFields with tableConfig.getPrimaryKey() == null.","commonSituations":"Declaring a sharded <table> with rule based on a column but omitting primaryKey=\"id\"; after adding a new table to schema.xml without the primaryKey attribute; autoincrement/sequence features requiring PK not configured.","solutions":["Add primaryKey=\"<column>\" to the table's <table> definition in schema.xml and reload the config","If PK routing is not needed, ensure the insert routing path does not rely on primaryKey (e.g. configure a proper sharding rule on an existing column)","Update the code/config so the table uses childTable/parent key routing only when the parent's primaryKey is defined"],"exampleFix":"// before (schema.xml)\n<table name=\"t_order\" dataNode=\"dn1,dn2\" rule=\"mod-rule\"/>\n// after\n<table name=\"t_order\" dataNode=\"dn1,dn2\" rule=\"mod-rule\" primaryKey=\"id\"/>","handlingStrategy":"validation","validationCode":"TableConfig tc = schema.getTables().get(tableName);\nif (tc == null || tc.getPrimaryKey() == null || tc.getPrimaryKey().isEmpty()) {\n  throw new IllegalStateException(\"table \" + tableName + \" needs primaryKey in schema.xml\");\n}","typeGuard":null,"tryCatchPattern":"try { routeInsert(...); } catch (RuntimeException e) { if (e.getMessage().contains(\"primaryKey\")) { /* fix schema.xml */ } throw e; }","preventionTips":["Always set primaryKey on sharded and ER child-table parents","Add schema.xml linting in CI","Reload and test config after every table addition"],"tags":["config","primary-key","routing"],"backgroundTag":"missing-required-config-field","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"}