{"record":{"id":"3d3fde407657a07b","repo":"yiisoft/yii2","slug":"unable-to-link-models-at-most-one-model-can-be-ne","errorCode":null,"errorMessage":"Unable to link models: at most one model can be newly created.","messagePattern":"Unable to link models: at most one model can be newly created\\.","errorType":"exception","errorClass":"yii\\base\\InvalidCallException","httpStatus":null,"severity":"error","filePath":"framework/db/BaseActiveRecord.php","lineNumber":1358,"sourceCode":"            if (is_array($relation->via)) {\n                /** @var ActiveRecordInterface $viaClass */\n                /** @var ActiveRecordInterface $record */\n                $record = Yii::createObject($viaClass);\n                foreach ($columns as $column => $value) {\n                    $record->$column = $value;\n                }\n                $record->insert(false);\n            } else {\n                /** @var string $viaTable */\n                static::getDb()->createCommand()->insert($viaTable, $columns)->execute();\n            }\n        } else {\n            $p1 = $model->isPrimaryKey(array_keys($relation->link));\n            $p2 = static::isPrimaryKey(array_values($relation->link));\n            if ($p1 && $p2) {\n                if ($this->getIsNewRecord()) {\n                    if ($model->getIsNewRecord()) {\n                        throw new InvalidCallException('Unable to link models: at most one model can be newly created.');\n                    }\n                    $this->bindModels(array_flip($relation->link), $this, $model);\n                } else {\n                    $this->bindModels($relation->link, $model, $this);\n                }\n            } elseif ($p1) {\n                $this->bindModels(array_flip($relation->link), $this, $model);\n            } elseif ($p2) {\n                $this->bindModels($relation->link, $model, $this);\n            } else {\n                throw new InvalidCallException('Unable to link models: the link defining the relation does not involve any primary key.');\n            }\n        }\n\n        // update lazily loaded related objects\n        if (!$relation->multiple) {\n            $this->_related[$name] = $model;\n        } elseif (isset($this->_related[$name])) {","sourceCodeStart":1340,"sourceCodeEnd":1376,"githubUrl":"https://github.com/yiisoft/yii2/blob/66f00d18a29b520f85e8e8f1e32d1e7e7b556cac/framework/db/BaseActiveRecord.php#L1340-L1376","documentation":"Error \"Unable to link models: at most one model can be newly created.\" thrown in yiisoft/yii2.","triggerScenarios":"Thrown at framework/db/BaseActiveRecord.php:1358 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"}