{"record":{"id":"f2859ffb7936027d","repo":"vitessio/vitess","slug":"no-primary-in-shard-record-v-v-consider-running","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, or providing a non-primary tablet alias","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, or providing a non-primary tablet alias","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"go/vt/wrangler/schema.go","lineNumber":193,"sourceCode":"\n// PreflightSchema will try a schema change on the remote tablet.\nfunc (wr *Wrangler) PreflightSchema(ctx context.Context, tabletAlias *topodatapb.TabletAlias, changes []string) ([]*tabletmanagerdatapb.SchemaChangeResult, error) {\n\tti, err := wr.ts.GetTablet(ctx, tabletAlias)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"GetTablet(%v) failed: %v\", tabletAlias, err)\n\t}\n\treturn wr.tmc.PreflightSchema(ctx, ti.Tablet, changes)\n}\n\n// CopySchemaShardFromShard copies the schema from a source shard to the specified destination shard.\n// For both source and destination it picks the primary tablet. See also CopySchemaShard.\nfunc (wr *Wrangler) CopySchemaShardFromShard(ctx context.Context, tables, excludeTables []string, includeViews bool, sourceKeyspace, sourceShard, destKeyspace, destShard string, waitReplicasTimeout time.Duration, skipVerify bool) error {\n\tsourceShardInfo, err := wr.ts.GetShard(ctx, sourceKeyspace, sourceShard)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"GetShard(%v, %v) failed: %v\", sourceKeyspace, sourceShard, err)\n\t}\n\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}","sourceCodeStart":175,"sourceCodeEnd":211,"githubUrl":"https://github.com/vitessio/vitess/blob/01a25a7d176f94613b8d59d799f438380a8760e4/go/vt/wrangler/schema.go#L175-L211","documentation":"CopySchemaShardFromShard requires the source shard record to contain a PrimaryAlias so it can fetch the schema from the source primary. This error is returned when the shard record exists but has no primary assigned — the shard was never initialized as primary or is mid-reparent with stale topo data. The message suggests remediation via InitShardPrimary or reparenting.","triggerScenarios":"Calling CopySchemaShardFromShard against a shard whose topology record has PrimaryAlias == nil: freshly created shard never primaried, shard whose primary was lost and not yet re-elected, or corrupted/stale topo data after a reparent.","commonSituations":"New shard created with CreateKeyspace but InitShardPrimary never run; failed reparent left the shard record without a primary; disaster recovery where the primary cell data was lost.","solutions":["Run vtctl InitShardPrimary <keyspace>/<shard> <tablet-alias> to assign a primary.","Or run a PlannedReparentShard/ElectivityReparentShard to restore a primary.","Alternatively pass a specific source tablet alias using CopySchemaShard instead of CopySchemaShardFromShard.","Verify with vtctl GetShard that PrimaryAlias is set before retrying."],"exampleFix":"// before (no primary)\nwr.CopySchemaShardFromShard(ctx, nil, nil, true, \"ks\", \"0\", \"ks\", \"-80\", timeout, false)\n// after: assign primary first, then copy\nvtctl InitShardPrimary ks/0 cell1-0000000100\nwr.CopySchemaShardFromShard(ctx, nil, nil, true, \"ks\", \"0\", \"ks\", \"-80\", timeout, false)","handlingStrategy":"validation","validationCode":"si, err := ts.GetShard(ctx, sourceKeyspace, sourceShard)\nif err != nil { return err }\nif si.PrimaryAlias == nil {\n\treturn fmt.Errorf(\"source shard %s/%s has no primary; run InitShardPrimary first\", sourceKeyspace, sourceShard)\n}","typeGuard":"func hasPrimary(si *topo.ShardInfo) bool { return si != nil && si.PrimaryAlias != nil }","tryCatchPattern":"err := wr.CopySchemaShardFromShard(...)\nif err != nil && strings.Contains(err.Error(), \"no primary in shard record\") {\n\t// remediate via vtctl InitShardPrimary or use CopySchemaShard with an explicit alias\n}","preventionTips":["Always run InitShardPrimary on new shards before schema operations","Check shard records (PrimaryAlias set) as a preflight step in automation","During reparents, verify primary is established before schema copies"],"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"}