{"record":{"id":"5e23800c479f7315","repo":"laravel/framework","slug":"morph-key-type-must-be-int-uuid-or-ulid","errorCode":null,"errorMessage":"Morph key type must be 'int', 'uuid', or 'ulid'.","messagePattern":"Morph key type must be 'int', 'uuid', or 'ulid'\\.","errorType":"exception","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"src/Illuminate/Database/Schema/Builder.php","lineNumber":99,"sourceCode":"     * Set the default time precision for migrations.\n     */\n    public static function defaultTimePrecision(?int $precision): void\n    {\n        static::$defaultTimePrecision = $precision;\n    }\n\n    /**\n     * Set the default morph key type for migrations.\n     *\n     * @param  string  $type\n     * @return void\n     *\n     * @throws \\InvalidArgumentException\n     */\n    public static function defaultMorphKeyType(string $type)\n    {\n        if (! in_array($type, ['int', 'uuid', 'ulid'])) {\n            throw new InvalidArgumentException(\"Morph key type must be 'int', 'uuid', or 'ulid'.\");\n        }\n\n        static::$defaultMorphKeyType = $type;\n    }\n\n    /**\n     * Set the default morph key type for migrations to UUIDs.\n     *\n     * @return void\n     */\n    public static function morphUsingUuids()\n    {\n        static::defaultMorphKeyType('uuid');\n    }\n\n    /**\n     * Set the default morph key type for migrations to ULIDs.\n     *","sourceCodeStart":81,"sourceCodeEnd":117,"githubUrl":"https://github.com/laravel/framework/blob/e0f6eb3518ac29fbbca8529e97d0df7fc9f24481/src/Illuminate/Database/Schema/Builder.php#L81-L117","documentation":"Schema\\Builder::defaultMorphKeyType is a static method that sets the global default morph key type for all migrations and polymorphic relationships. It validates that the type is one of 'int', 'uuid', or 'ulid' and throws InvalidArgumentException otherwise. This type determines the column definition for morphs()/nullableMorphs() and the key type stored in polymorphic morph_type columns.","triggerScenarios":"Calling Schema::defaultMorphKeyType('string'), Schema::defaultMorphKeyType('bigint'), or any value other than 'int', 'uuid', or 'ulid'. Typically called in AppServiceProvider::boot() or a migration file.","commonSituations":"Developer passes a type name that seems valid but is not in the allowed set (e.g., 'bigIncrements', 'string', 'guid'). Typo in the type string. Passing a variable that contains an unexpected value.","solutions":["Pass one of the three allowed values: Schema::defaultMorphKeyType('int'), 'uuid', or 'ulid'","Use the convenience methods: Schema::morphUsingUuids() or Schema::morphUsingUlids() instead of calling defaultMorphKeyType directly","If you need a custom key type, you cannot use defaultMorphKeyType — override morphs() calls individually with custom column definitions"],"exampleFix":"// before — invalid type throws\nSchema::defaultMorphKeyType('string');\n\n// after — use a supported type\nSchema::defaultMorphKeyType('uuid');\n// or use the convenience method\nSchema::morphUsingUuids();","handlingStrategy":"validation","validationCode":"$validTypes = ['int', 'uuid', 'ulid'];\n\nif (in_array($type, $validTypes, true)) {\n    Schema::defaultMorphKeyType($type);\n} else {\n    throw new \\InvalidArgumentException(\"Invalid morph key type: {$type}. Must be one of: \" . implode(', ', $validTypes));\n}","typeGuard":"function isValidMorphKeyType(string $type): bool {\n    return in_array($type, ['int', 'uuid', 'ulid'], true);\n}","tryCatchPattern":null,"preventionTips":["Use the convenience methods Schema::morphUsingUuids() or Schema::morphUsingUlids() instead of calling defaultMorphKeyType directly","Hard-code one of the three allowed string literals — do not pass dynamic/user input","Set morph key type once in AppServiceProvider::boot() and leave it unchanged for the application lifetime","Document the morph key type in project setup docs so all developers use the same type"],"tags":["morph","polymorphic","schema","uuid","ulid","validation"],"backgroundTag":null,"analyzedSha":"e0f6eb3518ac29fbbca8529e97d0df7fc9f24481","analyzedAt":"2026-08-11T20:52:37.562Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}