phalcon/cphalcon · error · Phalcon\Mvc\Model\Exceptions\PrimaryKeyRequired

A primary key must be defined in the model in order to…

Error message

A primary key must be defined in the model in order to perform the operation in '{className}'

What it means

Thrown when building dynamic SQL conditions (delete/update by primary key) and the model has no primary key attributes — metaData->getPrimaryKeyAttributes() returned empty. Operations keyed on the primary key require one; define a primary key for the model.

Solutions

  1. Define the primary key in the model's metadata (table primary key) or via the model's metaData method
  2. Choose a table with a primary key for deletable models
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at phalcon/Mvc/Model.zep:1546 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of phalcon/cphalcon@b7419de9cd (2026-08-21). Data as JSON: /api/errors/c6f5d81f4971b276. Report an issue: GitHub.

Appendix: source

Thrown at phalcon/Mvc/Model.zep:1546

        let values     = [],
            bindTypes  = [],
            conditions = [];

        let primaryKeys   = metaData->getPrimaryKeyAttributes(this),
            bindDataTypes = metaData->getBindTypes(this);

        if Settings::get("orm.column_renaming") {
            let columnMap = metaData->getColumnMap(this);
        } else {
            let columnMap = null;
        }

        /**
         * We can't create dynamic SQL without a primary key
         */
        if unlikely empty primaryKeys {
            throw new PrimaryKeyRequired(get_class(this));
        }

        /**
         * Create a condition from the primary keys
         */
        for primaryKey in primaryKeys {
            /**
             * Every column part of the primary key must be in the bind data
             * types
             */
            if unlikely !fetch bindType, bindDataTypes[primaryKey] {
                throw new BindTypeNotDefined(primaryKey, get_class(this));
            }

            /**
             * Take the column values based on the column map if any
             */
            if typeof columnMap == "array" {

View on GitHub (pinned to b7419de9cd)