{"record":{"id":"c642177584ca513f","repo":"vitessio/vitess","slug":"insert-failed-to-generate-an-id","errorCode":null,"errorMessage":"insert failed to generate an id","messagePattern":"insert failed to generate an id","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"go/vt/vttablet/tabletmanager/vreplication/engine.go","lineNumber":423,"sourceCode":"\t\treturn nil, err\n\t}\n\tdefer dbClient.Close()\n\n\t// Change the database to ensure that these events don't get\n\t// replicated by another vreplication. This can happen when\n\t// we reverse replication.\n\tif _, err := dbClient.ExecuteFetch(\"use \"+sidecar.GetIdentifier(), 1); err != nil {\n\t\treturn nil, err\n\t}\n\n\tswitch plan.opcode {\n\tcase insertQuery:\n\t\tqr, err := dbClient.ExecuteFetch(plan.query, 1)\n\t\tif err != nil {\n\t\t\treturn nil, err\n\t\t}\n\t\tif qr.InsertID == 0 {\n\t\t\treturn nil, errors.New(\"insert failed to generate an id\")\n\t\t}\n\t\tmaxInsert := qr.InsertID + uint64(plan.numInserts)\n\t\tif maxInsert > math.MaxInt32 {\n\t\t\treturn nil, fmt.Errorf(\"insert id %v out of range\", qr.InsertID)\n\t\t}\n\n\t\t// If we are creating multiple streams, for example in a\n\t\t// merge workflow going from 2 shards to 1 shard, we\n\t\t// will be inserting multiple rows. To get the ids of\n\t\t// subsequent streams we need to know what the\n\t\t// auto_increment_increment step is. In a multi-primary\n\t\t// environment, like a Galera cluster, for example,\n\t\t// we will often encounter auto_increment steps > 1.\n\t\tautoIncrementStep, err := vre.getAutoIncrementStep(dbClient)\n\t\tif err != nil {\n\t\t\treturn nil, err\n\t\t}\n\t\tfirstID := int32(qr.InsertID)","sourceCodeStart":405,"sourceCodeEnd":441,"githubUrl":"https://github.com/vitessio/vitess/blob/01a25a7d176f94613b8d59d799f438380a8760e4/go/vt/vttablet/tabletmanager/vreplication/engine.go#L405-L441","documentation":"The vreplication engine's exec, when executing an INSERT plan against the local _vt.vreplication-style tables, requires MySQL to report the generated auto-increment id (qr.InsertID). If the fetch succeeds but InsertID is 0, the insert did not generate an id and the engine aborts, since subsequent rows rely on computed ids (maxInsert = InsertID + numInserts).","triggerScenarios":"Executing an INSERT through vrengine exec where the target table lacks an AUTO_INCREMENT column or MySQL fails to report an insert id — e.g. inserts into tables not declared with auto-increment, or drivers/proxies that strip the last-insert-id info.","commonSituations":"Custom or patched binlogplayer DBClient implementations that don't populate InsertID; altering _vt tables to remove AUTO_INCREMENT; running against a backend (proxy/firewall) that doesn't return LAST_INSERT_ID properly.","solutions":["Ensure the inserted-into table has an AUTO_INCREMENT primary key as the vreplication schema expects","Use the standard binlogplayer DBClient implementation that returns a correct InsertID from ExecuteFetch","Re-apply the default Vitess _vt table schema (verify with the schema in go/vt/mysqlctl) if it was customized","Inspect any proxy between vttablet and MySQL for LAST_INSERT_ID handling"],"exampleFix":"// before\nCREATE TABLE _vt.vreplication (id INT PRIMARY KEY, ...) // no auto-increment\n// after\nCREATE TABLE _vt.vreplication (id INT AUTO_INCREMENT PRIMARY KEY, ...)","handlingStrategy":"validation","validationCode":"// ensure target _vt tables keep AUTO_INCREMENT ids before exec\nqr, err := dbClient.ExecuteFetch(\"show columns from _vt.vreplication like 'id'\", 10)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never remove AUTO_INCREMENT from _vt.vreplication-style tables","Use the stock binlogplayer DBClient implementation","Avoid proxies between vttablet and MySQL that strip insert ids"],"tags":["vreplication","insert","auto-increment"],"backgroundTag":"insert-id-missing","analyzedSha":"01a25a7d176f94613b8d59d799f438380a8760e4","analyzedAt":"2026-09-01T17:28:30.605Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}