{"record":{"id":"3c404f4516e81c6e","repo":"yiisoft/yii2","slug":"unable-to-link-models-the-primary-key-of-is-nu","errorCode":null,"errorMessage":"Unable to link models: the primary key of {} is null.","messagePattern":"Unable to link models: the primary key of (.+?) is null\\.","errorType":"exception","errorClass":"yii\\base\\InvalidCallException","httpStatus":null,"severity":"error","filePath":"framework/db/BaseActiveRecord.php","lineNumber":1596,"sourceCode":"                }\n            }\n        }\n\n        unset($this->_related[$name]);\n    }\n\n    /**\n     * @param array $link\n     * @param ActiveRecordInterface $foreignModel\n     * @param ActiveRecordInterface $primaryModel\n     * @throws InvalidCallException\n     */\n    private function bindModels($link, $foreignModel, $primaryModel)\n    {\n        foreach ($link as $fk => $pk) {\n            $value = $primaryModel->$pk;\n            if ($value === null) {\n                throw new InvalidCallException('Unable to link models: the primary key of ' . get_class($primaryModel) . ' is null.');\n            }\n            if (is_array($foreignModel->$fk)) { // relation via array valued attribute\n                $foreignModel->{$fk}[] = $value;\n            } else {\n                $foreignModel->{$fk} = $value;\n            }\n        }\n        $foreignModel->save(false);\n    }\n\n    /**\n     * Returns a value indicating whether the given set of attributes represents the primary key for this model.\n     * @param array $keys the set of attributes to check\n     * @return bool whether the given set of attributes represents the primary key for this model\n     */\n    public static function isPrimaryKey($keys)\n    {\n        $pks = static::primaryKey();","sourceCodeStart":1578,"sourceCodeEnd":1614,"githubUrl":"https://github.com/yiisoft/yii2/blob/66f00d18a29b520f85e8e8f1e32d1e7e7b556cac/framework/db/BaseActiveRecord.php#L1578-L1614","documentation":"Error \"Unable to link models: the primary key of {} is null.\" thrown in yiisoft/yii2.","triggerScenarios":"Thrown at framework/db/BaseActiveRecord.php:1596 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":[],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"66f00d18a29b520f85e8e8f1e32d1e7e7b556cac","analyzedAt":"2026-08-17T05:17:23.470Z","schemaVersion":2},"datasetVersion":"2026-08-17T09:17:11.063Z"}