{"record":{"id":"bd5660d8fcaf0df4","repo":"vitessio/vitess","slug":"vt09017","errorCode":"VT09017","errorMessage":"VT09017: INSERT with a target destination is not allowed","messagePattern":"VT09017: INSERT with a target destination is not allowed","errorType":"error_code","errorClass":"VitessError","httpStatus":null,"severity":"error","filePath":"go/vt/vtgate/planbuilder/operators/insert.go","lineNumber":363,"sourceCode":"\t\t}\n\t\tvar def sqlparser.Expr\n\t\tidx := ins.Columns.FindColumn(col.Name)\n\t\tif idx == -1 {\n\t\t\tdef = findDefault(vTbl, col.Name)\n\t\t\t// no default, replace it with null value.\n\t\t\tif def == nil {\n\t\t\t\tdef = &sqlparser.NullVal{}\n\t\t\t}\n\t\t}\n\t\toffsets = append(offsets, uComp{idx, def})\n\t\treturn false, nil\n\t}, expr)\n\treturn offsets, false\n}\n\nfunc createInsertOperator(ctx *plancontext.PlanningContext, insStmt *sqlparser.Insert, vTbl *vindexes.BaseTable, routing Routing) (op Operator) {\n\tif _, target := routing.(*TargetedRouting); target {\n\t\tpanic(vterrors.VT09017(\"INSERT with a target destination is not allowed\"))\n\t}\n\n\tinsOp := &Insert{\n\t\tVTable: vTbl,\n\t\tAST:    insStmt,\n\t}\n\troute := &Route{\n\t\tunaryOperator: newUnaryOp(insOp),\n\t\tRouting:       routing,\n\t}\n\n\t// Table column list is nil then add all the columns\n\t// If the column list is empty then add only the auto-inc column and\n\t// this happens on calling modifyForAutoinc\n\tif insStmt.Columns == nil && valuesProvided(insStmt.Rows) {\n\t\tif vTbl.ColumnListAuthoritative {\n\t\t\tinsStmt = populateInsertColumnlist(insStmt, vTbl)\n\t\t} else {","sourceCodeStart":345,"sourceCodeEnd":381,"githubUrl":"https://github.com/vitessio/vitess/blob/01a25a7d176f94613b8d59d799f438380a8760e4/go/vt/vtgate/planbuilder/operators/insert.go#L345-L381","documentation":"VT09017 is raised by createInsertOperator in go/vt/vtgate/planbuilder/operators/insert.go:363 when an INSERT statement targets a explicitly-set destination (a targeted routing) instead of a keyspace/table resolved through normal routing. Vitess does not support combining a route target (e.g. from USE statements with @primary/@replica or vtgate target syntax) with INSERT planning, so it rejects the statement.","triggerScenarios":"Executing an INSERT while the session has a targeted destination (routing is *TargetedRouting) — e.g. after `USE ks@primary` or sending the query with an explicit tablet-type/tablet-target destination to vtgate.","commonSituations":"Scripts that set a tablet target for reads and reuse the same session for writes; tools pinning traffic to a specific replica or tablet type; testing setups that redirect all statements to a chosen destination.","solutions":["Remove the explicit target: use `USE keyspace` (without @primary/@replica suffix) before the INSERT and let VTGate route the write","Send the INSERT to a tablet directly (vttablet) instead of vtgate if you need tablet-level targeting","Split the workload: use targeted sessions for reads and a normal session for DML"],"exampleFix":"// before\nUSE commerce@replica;\nINSERT INTO products (id, name) VALUES (1, 'x');\n// after\nUSE commerce;\nINSERT INTO products (id, name) VALUES (1, 'x');","handlingStrategy":"validation","validationCode":"// Reject targeted destinations before sending DML\nif (/^[^\\s]+@(primary|replica|rdonly)$/.test(currentUseTarget)) {\n  throw new Error('DML not allowed on targeted destination: ' + currentUseTarget);\n}","typeGuard":"function isTargetedDestination(target) {\n  return typeof target === 'string' && /@[a-z]+$/i.test(target);\n}","tryCatchPattern":"try {\n  await conn.query('INSERT INTO t VALUES (?)', [v]);\n} catch (e) {\n  if (String(e).includes('VT09017')) {\n    await conn.query('USE commerce'); // drop the @tablet-type suffix and retry\n  } else throw e;\n}","preventionTips":["Use `USE keyspace` without @primary/@replica suffixes for write sessions","Keep read-targeting and write traffic on separate connections","Send tablet-targeted DML to vttablet directly, not through vtgate"],"tags":["insert","routing","targeted-destination"],"backgroundTag":"unsupported-targeted-destination","analyzedSha":"01a25a7d176f94613b8d59d799f438380a8760e4","analyzedAt":"2026-09-01T17:28:30.605Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}