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

Cannot delete the record because the primary key attribute

Error message

Cannot delete the record because the primary key attribute: '{attribute}' was not set in '{className}'

What it means

Thrown while building delete conditions when a primary key attribute is not set on the model instance, so the record cannot be uniquely identified for deletion. Load/populate the primary key before deleting.

Solutions

  1. Load the record fully (findFirst) before deleting so PK attributes are populated
  2. Manually set the primary key attribute(s) before delete()
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at phalcon/Mvc/Model.zep:1577 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/945b397938dd5176. Report an issue: GitHub.

Appendix: source

Thrown at phalcon/Mvc/Model.zep:1577

            }

            /**
             * Take the column values based on the column map if any
             */
            if typeof columnMap == "array" {
                if unlikely !fetch attributeField, columnMap[primaryKey] {
                    throw new ColumnNotInTableMap(primaryKey, get_class(this));
                }
            } else {
                let attributeField = primaryKey;
            }

            /**
             * If the attribute is currently set in the object add it to the
             * conditions
             */
            if unlikely !fetch value, this->{attributeField} {
                throw new PrimaryKeyAttributeNotSet(attributeField, get_class(this));
            }

            /**
             * Escape the column identifier
             */
            let values[] = value,
                conditions[] = writeConnection->escapeIdentifier(primaryKey) . " = ?",
                bindTypes[] = bindType;
        }

        if Settings::get("orm.events") {
            let this->skipped = false;

            /**
             * Fire the beforeDelete event
             */
            if this->fireEventCancel("beforeDelete") === false {
                return false;

View on GitHub (pinned to b7419de9cd)