{"record":{"id":"518c4edbb587fb27","repo":"hashicorp/terraform","slug":"schema-version-d-for-s-in-state-does-not-match-v","errorCode":null,"errorMessage":"schema version %d for %s in state does not match version %d from the provider","messagePattern":"schema version (.+?) for (.+?) in state does not match version (.+?) from the provider","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/command/jsonstate/state.go","lineNumber":421,"sourceCode":"\t\t\tcase addrs.DataResourceMode:\n\t\t\t\tcurrent.Mode = DataResourceMode\n\t\t\tdefault:\n\t\t\t\treturn ret, fmt.Errorf(\"resource %s has an unsupported mode %s\",\n\t\t\t\t\tresAddr.String(),\n\t\t\t\t\tresAddr.Mode.String(),\n\t\t\t\t)\n\t\t\t}\n\n\t\t\tschema := schemas.ResourceTypeConfig(\n\t\t\t\tr.ProviderConfig.Provider,\n\t\t\t\tresAddr.Mode,\n\t\t\t\tresAddr.Type,\n\t\t\t)\n\n\t\t\t// It is possible that the only instance is deposed\n\t\t\tif ri.Current != nil {\n\t\t\t\tif schema.Version != int64(ri.Current.SchemaVersion) {\n\t\t\t\t\treturn nil, fmt.Errorf(\"schema version %d for %s in state does not match version %d from the provider\", ri.Current.SchemaVersion, resAddr, schema.Version)\n\t\t\t\t}\n\n\t\t\t\tcurrent.SchemaVersion = ri.Current.SchemaVersion\n\n\t\t\t\tif schema.Body == nil {\n\t\t\t\t\treturn nil, fmt.Errorf(\"no schema found for %s (in provider %s)\", resAddr.String(), r.ProviderConfig.Provider)\n\t\t\t\t}\n\n\t\t\t\t// Check if we have an identity in the state\n\t\t\t\tif ri.Current.IdentityJSON != nil {\n\t\t\t\t\tif schema.IdentityVersion != int64(ri.Current.IdentitySchemaVersion) {\n\t\t\t\t\t\treturn nil, fmt.Errorf(\"resource identity schema version %d for %s in state does not match version %d from the provider\", ri.Current.IdentitySchemaVersion, resAddr, schema.IdentityVersion)\n\t\t\t\t\t}\n\n\t\t\t\t\tif schema.Identity == nil {\n\t\t\t\t\t\treturn nil, fmt.Errorf(\"no resource identity schema found for %s (in provider %s)\", resAddr.String(), r.ProviderConfig.Provider)\n\t\t\t\t\t}\n","sourceCodeStart":403,"sourceCodeEnd":439,"githubUrl":"https://github.com/hashicorp/terraform/blob/d32a084675427f5ac3f7d2868578ef8b2c1dc525/internal/command/jsonstate/state.go#L403-L439","documentation":"Thrown by jsonstate when the schema version stored on the resource instance in state (ri.Current.SchemaVersion) does not equal the schema version the currently loaded provider exposes (schema.Version). Terraform refuses to serialize JSON state from a version it cannot safely interpret, because attribute layout may differ between schema versions.","triggerScenarios":"State was written by a different provider version than the one now configured; a provider upgraded its schema version but the state-upgrade step did not run (e.g. state loaded via `state pull`/`state push` without a refresh); provider downgrade.","commonSituations":"Downgrading a provider after state was written by a newer one; restoring a state backup from a newer Terraform/provider stack; pointing Terraform at a different provider source whose schema versioning diverged; manually edited state files.","solutions":["Run `terraform apply -refresh-only` (or `terraform refresh`) so the provider runs its state upgrader to the current schema version.","Align the provider version in required_providers with the version that wrote the state.","If the state is from a newer provider and downgrade is required, use `terraform state replace-provider` or migrate the resource to a compatible provider.","Restore the state from a backup that matches the configured provider version."],"exampleFix":"# before\nrequired_providers { aws = { version = \"~> 4.0\" } }  # state written by 5.x\n\n# after\nrequired_providers { aws = { version = \"~> 5.0\" } }  # then terraform init && terraform apply -refresh-only","handlingStrategy":"validation","validationCode":"// Compare the stored schema version against the provider's before marshaling.\nif int64(ri.Current.SchemaVersion) != schema.Version {\n    return fmt.Errorf(\"state schema version %d for %s is stale (provider has %d); run 'terraform apply -refresh-only'\", ri.Current.SchemaVersion, addr, schema.Version)\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Run `terraform apply -refresh-only` after any provider version change to upgrade state.","Keep provider versions aligned with the provenance of your state backups.","Avoid editing state files by hand; use `terraform state` subcommands."],"tags":["terraform","json-state","schema-version","provider","state-migration","version-drift"],"backgroundTag":null,"analyzedSha":"d32a084675427f5ac3f7d2868578ef8b2c1dc525","analyzedAt":"2026-08-11T18:43:52.779Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}