{"record":{"id":"e78f42c2d5eea373","repo":"vitessio/vitess","slug":"no-primary-in-shard-record-v-v-consider-running-e78f42","errorCode":null,"errorMessage":"no primary in shard record %v/%v. Consider running 'vtctl InitShardPrimary' in case of a new shard or reparenting the shard to fix the topology data","messagePattern":"no primary in shard record (.+?)/(.+?)\\. Consider running 'vtctl InitShardPrimary' in case of a new shard or reparenting the shard to fix the topology data","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"go/vt/wrangler/schema.go","lineNumber":210,"sourceCode":"\tif sourceShardInfo.PrimaryAlias == nil {\n\t\treturn fmt.Errorf(\"no primary in shard record %v/%v. Consider running 'vtctl InitShardPrimary' in case of a new shard or reparenting the shard to fix the topology data, or providing a non-primary tablet alias\", sourceKeyspace, sourceShard)\n\t}\n\n\treturn wr.CopySchemaShard(ctx, sourceShardInfo.PrimaryAlias, tables, excludeTables, includeViews, destKeyspace, destShard, waitReplicasTimeout, skipVerify)\n}\n\n// CopySchemaShard copies the schema from a source tablet to the\n// specified shard.  The schema is applied directly on the primary of\n// the destination shard, and is propagated to the replicas through\n// binlogs.\nfunc (wr *Wrangler) CopySchemaShard(ctx context.Context, sourceTabletAlias *topodatapb.TabletAlias, tables, excludeTables []string, includeViews bool, destKeyspace, destShard string, waitReplicasTimeout time.Duration, skipVerify bool) error {\n\tdestShardInfo, err := wr.ts.GetShard(ctx, destKeyspace, destShard)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"GetShard(%v, %v) failed: %v\", destKeyspace, destShard, err)\n\t}\n\n\tif destShardInfo.PrimaryAlias == nil {\n\t\treturn fmt.Errorf(\"no primary in shard record %v/%v. Consider running 'vtctl InitShardPrimary' in case of a new shard or reparenting the shard to fix the topology data\", destKeyspace, destShard)\n\t}\n\n\tdiffs, err := schematools.CompareSchemas(ctx, wr.ts, wr.tmc, sourceTabletAlias, destShardInfo.PrimaryAlias, tables, excludeTables, includeViews)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"CopySchemaShard failed because schemas could not be compared initially: %v\", err)\n\t}\n\tif diffs == nil {\n\t\t// Return early because dest has already the same schema as source.\n\t\treturn nil\n\t}\n\n\treq := &tabletmanagerdatapb.GetSchemaRequest{Tables: tables, ExcludeTables: excludeTables, IncludeViews: includeViews}\n\tsourceSd, err := schematools.GetSchema(ctx, wr.ts, wr.tmc, sourceTabletAlias, req)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"GetSchema(%v, %v, %v, %v) failed: %v\", sourceTabletAlias, tables, excludeTables, includeViews, err)\n\t}\n\n\tcreateSQLstmts := tmutils.SchemaDefinitionToSQLStrings(sourceSd)","sourceCodeStart":192,"sourceCodeEnd":228,"githubUrl":"https://github.com/vitessio/vitess/blob/01a25a7d176f94613b8d59d799f438380a8760e4/go/vt/wrangler/schema.go#L192-L228","documentation":"CopySchemaShard requires the destination shard to have a PrimaryAlias, because the schema is applied directly on the destination primary and propagates to replicas via binlogs. This error is returned when the destination shard record exists but PrimaryAlias == nil — no primary is currently assigned. The message recommends InitShardPrimary or reparenting.","triggerScenarios":"Calling CopySchemaShard against a destination shard whose topology record has no primary: brand-new shard never primaried, primary lost after failure and not re-elected, or stale topo data after reparenting.","commonSituations":"Setting up a new shard intended as a schema-copy target but skipping InitShardPrimary; shard in emergency failover state; topo record corrupted after a manual reparent attempt.","solutions":["Run vtctl InitShardPrimary <destKeyspace>/<destShard> <tablet-alias> to assign a primary.","Or perform PlannedReparentShard to restore the primary.","Verify with vtctl GetShard that PrimaryAlias is populated before retrying CopySchemaShard.","If a primary is not desired, copy schema to a specific tablet using alternative tooling."],"exampleFix":"// before (dest has no primary)\nwr.CopySchemaShard(ctx, srcAlias, nil, nil, true, \"ks\", \"-80\", timeout, false)\n// after\nvtctl InitShardPrimary ks/-80 cell1-0000000200\nwr.CopySchemaShard(ctx, srcAlias, nil, nil, true, \"ks\", \"-80\", timeout, false)","handlingStrategy":"validation","validationCode":"si, err := ts.GetShard(ctx, destKeyspace, destShard)\nif err != nil { return err }\nif si.PrimaryAlias == nil {\n\treturn fmt.Errorf(\"dest shard %s/%s has no primary; run InitShardPrimary first\", destKeyspace, destShard)\n}","typeGuard":"func hasPrimary(si *topo.ShardInfo) bool { return si != nil && si.PrimaryAlias != nil }","tryCatchPattern":"err := wr.CopySchemaShard(...)\nif err != nil && strings.Contains(err.Error(), \"no primary in shard record\") {\n\t// run InitShardPrimary / PlannedReparentShard then retry\n}","preventionTips":["Initialize primaries on target shards as part of shard provisioning","Preflight shard records in automation before schema copy workflows","Monitor for shards without primaries via topology audits"],"tags":["topology","primary-election","shard-record","copy-schema"],"backgroundTag":"no-primary-in-shard","analyzedSha":"01a25a7d176f94613b8d59d799f438380a8760e4","analyzedAt":"2026-09-01T17:28:30.605Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}