{"record":{"id":"9ce2ccbffa0de818","repo":"laravel/framework","slug":"no-primary-key-defined-on-model","errorCode":null,"errorMessage":"No primary key defined on model.","messagePattern":"No primary key defined on model\\.","errorType":"exception","errorClass":"LogicException","httpStatus":null,"severity":"error","filePath":"src/Illuminate/Database/Eloquent/Model.php","lineNumber":1742,"sourceCode":"            }\n        }\n\n        return $count;\n    }\n\n    /**\n     * Delete the model from the database.\n     *\n     * @return bool|null\n     *\n     * @throws \\LogicException\n     */\n    public function delete()\n    {\n        $this->mergeAttributesFromCachedCasts();\n\n        if (is_null($this->getKeyName())) {\n            throw new LogicException('No primary key defined on model.');\n        }\n\n        // If the model doesn't exist, there is nothing to delete so we'll just return\n        // immediately and not do anything else. Otherwise, we will continue with a\n        // deletion process on the model, firing the proper events, and so forth.\n        if (! $this->exists) {\n            return;\n        }\n\n        if ($this->fireModelEvent('deleting') === false) {\n            return false;\n        }\n\n        // Here, we'll touch the owning models, verifying these timestamps get updated\n        // for the models. This will allow any caching to get broken on the parents\n        // by the timestamp. Then we will go ahead and delete the model instance.\n        $this->touchOwners();\n","sourceCodeStart":1724,"sourceCodeEnd":1760,"githubUrl":"https://github.com/laravel/framework/blob/e0f6eb3518ac29fbbca8529e97d0df7fc9f24481/src/Illuminate/Database/Eloquent/Model.php#L1724-L1760","documentation":"delete() throws when getKeyName() returns null, i.e. the model declares no primary key (protected $primaryKey = null or it was never set and the base default was removed). Without a primary key Eloquent cannot build the DELETE WHERE clause, so it refuses rather than issue a dangerous table-wide delete.","triggerScenarios":"Defining a model with protected $primaryKey = null (or unset), or a model that intentionally has no PK column (e.g. a view), then calling $model->delete().","commonSituations":"Models backed by database views or junction tables without a PK; overriding $primaryKey to an empty value by mistake; dynamic configuration that nulls the key; soft-delete on a PK-less model.","solutions":["Set a real primary key column: protected $primaryKey = 'id';.","If the table genuinely has no PK, perform the delete via the query builder: DB::table('table')->where(...)->delete();.","For composite keys, this guard still applies - Eloquent's delete() needs a single key; override delete() in the model to build the WHERE manually."],"exampleFix":"// before\nclass UserView extends Model\n{\n    protected $table = 'user_view';\n    protected $primaryKey = null;\n}\n$view->delete(); // throws\n\n// after - delete via query builder\nDB::table('user_view')->where('user_id', $id)->delete();","handlingStrategy":"validation","validationCode":"if (! $model->getKeyName()) {\n    // delete via the query builder instead\n    return \\DB::table($model->getTable())->where($where)->delete();\n}\nreturn $model->delete();","typeGuard":"function hasPrimaryKey(\\Illuminate\\Database\\Eloquent\\Model $m): bool {\n    return $m->getKeyName() !== null && $m->getKeyName() !== '';\n}","tryCatchPattern":"try {\n    $model->delete();\n} catch (\\LogicException $e) {\n    if (str_contains($e->getMessage(), 'No primary key')) {\n        \\DB::table($model->getTable())->where('id', $model->id)->delete();\n    } else { throw $e; }\n}","preventionTips":["Set protected $primaryKey on models backed by real tables.","For view/junction models without a PK, delete via DB::table() with an explicit WHERE.","Audit models where you intentionally null $primaryKey to ensure no delete() calls reach them."],"tags":["eloquent","delete","primary-key","model","query-builder"],"backgroundTag":null,"analyzedSha":"e0f6eb3518ac29fbbca8529e97d0df7fc9f24481","analyzedAt":"2026-08-11T20:52:37.562Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}