{"record":{"id":"34b2e53b51054000","repo":"mongodb/laravel-mongodb","slug":"constant-s-schema-version-is-required-when-using","errorCode":null,"errorMessage":"Constant %s::SCHEMA_VERSION is required when using HasSchemaVersion","messagePattern":"Constant (.+?)::SCHEMA_VERSION is required when using HasSchemaVersion","errorType":"exception","errorClass":"LogicException","httpStatus":null,"severity":"error","filePath":"src/Eloquent/HasSchemaVersion.php","lineNumber":79,"sourceCode":"    /**\n     * Get Current document version, fallback to 0 if not set\n     */\n    public function getSchemaVersion(): int\n    {\n        return $this->{static::getSchemaVersionKey()} ?? 0;\n    }\n\n    protected static function getSchemaVersionKey(): string\n    {\n        return 'schema_version';\n    }\n\n    protected function getModelSchemaVersion(): int\n    {\n        try {\n            return $this::SCHEMA_VERSION;\n        } catch (Error) {\n            throw new LogicException(sprintf('Constant %s::SCHEMA_VERSION is required when using HasSchemaVersion', $this::class));\n        }\n    }\n}\n","sourceCodeStart":61,"sourceCodeEnd":83,"githubUrl":"https://github.com/mongodb/laravel-mongodb/blob/0634653039468ceb0268a69192bdac64469ed043/src/Eloquent/HasSchemaVersion.php#L61-L83","documentation":"The HasSchemaVersion trait requires the consuming model class to define a SCHEMA_VERSION class constant, used to stamp documents with a schema version. getModelSchemaVersion() catches the Error from accessing an undefined constant and rethrows a clearer LogicException naming the class.","triggerScenarios":"Using the HasSchemaVersion trait on a model class that does not declare `public const SCHEMA_VERSION = <int>;`.","commonSituations":"Adding the trait via refactor and forgetting the constant; copying the trait to a new model; renaming/refactoring that removed the constant while the trait remained.","solutions":["Declare `public const SCHEMA_VERSION = 1;` (or the appropriate int) on the model class","Remove the HasSchemaVersion trait if schema versioning is not needed","Check that the constant is defined on the class using the trait, not a parent only when required"],"exampleFix":"// before\nclass Invoice extends Document { use HasSchemaVersion; }\n// after\nclass Invoice extends Document {\n    use HasSchemaVersion;\n    public const SCHEMA_VERSION = 1;\n}","handlingStrategy":"validation","validationCode":"if (in_array(HasSchemaVersion::class, class_uses_recursive($modelClass), true) && !defined($modelClass . '::SCHEMA_VERSION')) {\n    throw new RuntimeException($modelClass . ' must define SCHEMA_VERSION');\n}","typeGuard":"function definesSchemaVersion(string $modelClass): bool {\n    return defined($modelClass . '::SCHEMA_VERSION')\n        && in_array(\\MongoDB\\Laravel\\Eloquent\\HasSchemaVersion::class, class_uses_recursive($modelClass), true);\n}","tryCatchPattern":"try {\n    $model = new Invoice();\n} catch (LogicException $e) {\n    if (str_contains($e->getMessage(), 'SCHEMA_VERSION is required')) {\n        Log::error('Model using HasSchemaVersion is missing the SCHEMA_VERSION constant');\n    } else {\n        throw $e;\n    }\n}","preventionTips":["Define SCHEMA_VERSION immediately whenever adding the trait","Add a unit test asserting each HasSchemaVersion model defines the constant","Grep codebase for 'use HasSchemaVersion' during reviews to verify constants exist"],"tags":["eloquent","schema-version","trait"],"backgroundTag":"missing-required-config-field","analyzedSha":"0634653039468ceb0268a69192bdac64469ed043","analyzedAt":"2026-09-15T02:56:37.067Z","contentChangedAt":"2026-09-15T02:56:37.067Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}