{"record":{"id":"f0c4fb67f19cb7b3","repo":"vitessio/vitess","slug":"source-shard-must-have-a-primary-for-copying-schem","errorCode":null,"errorMessage":"source shard must have a primary for copying schema: %v","messagePattern":"source shard must have a primary for copying schema: (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"go/vt/wrangler/materializer.go","lineNumber":1137,"sourceCode":"\n\treturn &materializer{\n\t\twr:                    wr,\n\t\tms:                    ms,\n\t\ttargetVSchema:         targetVSchema,\n\t\tsourceShards:          sourceShards,\n\t\ttargetShards:          targetShards,\n\t\tisPartial:             isPartial,\n\t\tprimaryVindexesDiffer: differentPVs,\n\t}, nil\n}\n\nfunc (mz *materializer) getSourceTableDDLs(ctx context.Context) (map[string]string, error) {\n\tsourceDDLs := make(map[string]string)\n\tallTables := []string{\"/.*/\"}\n\n\tsourcePrimary := mz.sourceShards[0].PrimaryAlias\n\tif sourcePrimary == nil {\n\t\treturn nil, fmt.Errorf(\"source shard must have a primary for copying schema: %v\", mz.sourceShards[0].ShardName())\n\t}\n\n\tti, err := mz.wr.sourceTs.GetTablet(ctx, sourcePrimary)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\treq := &tabletmanagerdatapb.GetSchemaRequest{Tables: allTables}\n\tsourceSchema, err := mz.wr.tmc.GetSchema(ctx, ti.Tablet, req)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\tfor _, td := range sourceSchema.TableDefinitions {\n\t\tsourceDDLs[td.Name] = td.Schema\n\t}\n\treturn sourceDDLs, nil\n}\n","sourceCodeStart":1119,"sourceCodeEnd":1155,"githubUrl":"https://github.com/vitessio/vitess/blob/01a25a7d176f94613b8d59d799f438380a8760e4/go/vt/wrangler/materializer.go#L1119-L1155","documentation":"During MoveTables/LiveMigration schema copy, the materializer must fetch the source table DDLs from a source shard's primary tablet. This error is thrown by getSourceTableDDLs when the first source shard has no PrimaryAlias, meaning Vitess does not know of a serving primary in that shard, so schema cannot be copied.","triggerScenarios":"Running a materialization workflow (MoveTables, Reshard-backed materialization) with stopWorld or schema copy where the source keyspace's first shard has no elected primary — e.g. source keyspace is externally managed, primary is down/reparenting, or the shard has only replica/rdonly tablets.","commonSituations":"User points materialization at a source keyspace whose primary was just failed over and topo not yet updated; source keyspace left without a primary after disaster recovery; table CreateDdl set to 'copy' so deploySchema lazily calls getSourceTableDDLs on a primary-less shard.","solutions":["Ensure the source shard has a healthy primary: run `vtctldclient PlannedReparentShard <keyspace>/<shard>` or PromoteReplica to elect one","Wait for the topology to reflect the new primary and re-run the workflow","If the source keyspace cannot have a primary, provide an explicit CreateDdl statement per table instead of 'copy'","Check `vtctldclient GetShard <keyspace>/<shard>` to confirm PrimaryAlias is set before starting the workflow"],"exampleFix":"// before (vtctldclient)\nvtctldclient MoveTables --target-keyspace=commerce create --source-keyspace=inventory --tables='t1'\n// after: first ensure a primary exists\nvtctldclient GetShard commerce/0        # verify PrimaryAlias\nvtctldclient PlannedReparentShard commerce/0 --new-primary=zone1-101\nvtctldclient MoveTables --target-keyspace=commerce create --source-keyspace=inventory --tables='t1'","handlingStrategy":"validation","validationCode":"si, err := wr.ts.GetShard(ctx, sourceKeyspace, sourceShard)\nif err != nil { return err }\nif !si.HasPrimary() {\n    return fmt.Errorf(\"source shard %s/%s has no primary; reparent first\", sourceKeyspace, sourceShard)\n}","typeGuard":"func shardHasPrimary(si *topo.ShardInfo) bool { return si != nil && si.HasPrimary() }","tryCatchPattern":null,"preventionTips":["Check `vtctldclient GetShard` for PrimaryAlias before starting any MoveTables/materialization","Run PlannedReparentShard promptly after primary failures and verify topo convergence","Avoid 'copy' CreateDdl for sources that may lack primaries; supply explicit DDL"],"tags":["vitess","wrangler","topology","schema-copy"],"backgroundTag":"shard-missing-primary","analyzedSha":"01a25a7d176f94613b8d59d799f438380a8760e4","analyzedAt":"2026-09-01T17:28:30.605Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}