{"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":"console","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/c9def3e214014c1188faabfc4a5bde5095139765/internal/command/jsonstate/state.go#L403-L439","documentation":"Thrown when the schema version recorded in state for a resource instance does not match the schema version the currently-loaded provider reports for that resource type. Terraform uses schema versions to drive state upgraders; a mismatch means the provider cannot safely decode the stored object.","triggerScenarios":"Occurs in jsonstate when ri.Current.SchemaVersion != schema.Version (provider's). Triggered by JSON state rendering for a resource whose provider version changed since the state was written (e.g. provider downgraded, or upgraded without running the state upgraders).","commonSituations":"Most common after a provider downgrade (state was written by provider v2 schema but loaded provider is v1), after restoring an old state file with a newer provider, or when required_providers pins an incompatible version. Also seen with manually edited state files.","solutions":["Match the provider version to what wrote the state: set required_providers to the version that last successfully applied, then `terraform init`.","If upgrading, ensure Terraform runs the resource instance state upgraders by applying with the newer provider rather than just rendering JSON.","If downgrading is intentional, downgrade the provider in lock file and re-init.","For an unrecoverable mismatch, `terraform state rm` the resource, then re-apply/re-import to rewrite state with the current schema."],"exampleFix":"// before: provider downgraded below state schema version\nterraform {\n  required_providers {\n    aws = { source = \"hashicorp/aws\", version = \"= 3.0.0\" }\n  }\n}\n// after: restore version that matches state\nterraform {\n  required_providers {\n    aws = { source = \"hashicorp/aws\", version = \"~> 5.0\" }\n  }\n}","handlingStrategy":"validation","validationCode":"// Before JSON state rendering, verify each resource's state schema version matches the provider.\nfor _, r := range state.Resources {\n    s := schemas.ResourceTypeConfig(r.ProviderConfig.Provider, r.Addr.Mode, r.Addr.Type)\n    if r.Current != nil && int64(r.Current.SchemaVersion) != s.Version {\n        return fmt.Errorf(\"schema drift for %s: state=%d provider=%d — re-apply or pin provider\", r.Addr, r.Current.SchemaVersion, s.Version)\n    }\n}","typeGuard":null,"tryCatchPattern":"if _, err := jsonstate.MarshalState(state, schemas); err != nil {\n    // On schema-version mismatch, re-apply with the matching provider instead of rendering.\n    return err\n}","preventionTips":["Keep required_providers versions consistent across environments and over time.","Run `terraform apply` (not just show) after provider upgrades so state upgraders run.","Commit the .terraform.lock.hcl file so provider versions are reproducible."],"tags":["terraform","jsonstate","schema-version","version-mismatch","state"],"analyzedSha":"c9def3e214014c1188faabfc4a5bde5095139765","analyzedAt":"2026-08-07T15:39:49.278Z","schemaVersion":2},"datasetVersion":"2026-08-07T20:17:04.800Z"}