yiisoft/yii2 · error · yii\base\InvalidArgumentException

Primary key '$key' cannot be found in table '{$this->name}'.

Error message

Primary key '$key' cannot be found in table '{$this->name}'.

What it means

Error "Primary key '$key' cannot be found in table '{$this->name}'." thrown in yiisoft/yii2.

Source

Thrown at framework/db/TableSchema.php:100

    }

    /**
     * Manually specifies the primary key for this table.
     * @param string|array $keys the primary key (can be composite)
     * @throws InvalidArgumentException if the specified key cannot be found in the table.
     */
    public function fixPrimaryKey($keys)
    {
        $keys = (array) $keys;
        $this->primaryKey = $keys;
        foreach ($this->columns as $column) {
            $column->isPrimaryKey = false;
        }
        foreach ($keys as $key) {
            if (isset($this->columns[$key])) {
                $this->columns[$key]->isPrimaryKey = true;
            } else {
                throw new InvalidArgumentException("Primary key '$key' cannot be found in table '{$this->name}'.");
            }
        }
    }
}

View on GitHub (pinned to 66f00d18a2)

When it happens

Trigger: Thrown at framework/db/TableSchema.php:100 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of yiisoft/yii2@66f00d18a2 (2026-08-17). Data as JSON: /api/errors/e2749d27c5e5eb50. Report an issue: GitHub.