yiisoft/yii2 · error · yii\base\InvalidCallException

Unable to link models: at most one model can be newly create

Error message

Unable to link models: at most one model can be newly created.

What it means

Error "Unable to link models: at most one model can be newly created." thrown in yiisoft/yii2.

Source

Thrown at framework/db/BaseActiveRecord.php:1358

            if (is_array($relation->via)) {
                /** @var ActiveRecordInterface $viaClass */
                /** @var ActiveRecordInterface $record */
                $record = Yii::createObject($viaClass);
                foreach ($columns as $column => $value) {
                    $record->$column = $value;
                }
                $record->insert(false);
            } else {
                /** @var string $viaTable */
                static::getDb()->createCommand()->insert($viaTable, $columns)->execute();
            }
        } else {
            $p1 = $model->isPrimaryKey(array_keys($relation->link));
            $p2 = static::isPrimaryKey(array_values($relation->link));
            if ($p1 && $p2) {
                if ($this->getIsNewRecord()) {
                    if ($model->getIsNewRecord()) {
                        throw new InvalidCallException('Unable to link models: at most one model can be newly created.');
                    }
                    $this->bindModels(array_flip($relation->link), $this, $model);
                } else {
                    $this->bindModels($relation->link, $model, $this);
                }
            } elseif ($p1) {
                $this->bindModels(array_flip($relation->link), $this, $model);
            } elseif ($p2) {
                $this->bindModels($relation->link, $model, $this);
            } else {
                throw new InvalidCallException('Unable to link models: the link defining the relation does not involve any primary key.');
            }
        }

        // update lazily loaded related objects
        if (!$relation->multiple) {
            $this->_related[$name] = $model;
        } elseif (isset($this->_related[$name])) {

View on GitHub (pinned to 66f00d18a2)

When it happens

Trigger: Thrown at framework/db/BaseActiveRecord.php:1358 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/3d3fde407657a07b. Report an issue: GitHub.