Added new field to an AR model, old migration now fails

I may have worked myself into a corner on this. Would love feedback.

In a lot of our migrations, we use the class itself and not insert() to add records. There are a couple of reasons for this, including it was easier if we want to populate a uuid column when saving to the db.

This hasn’t caused any issues until recently.

When we test a branch, we do a migrate/fresh and work through any migration issues. Fine. But a recent change to a model has impacted an OLD migration file. In production, this in theory won’t cause issues since that migration is a few months ago. But it totally bombs a migrate/fresh, and so we have a QA issue.

The real solution may be to stop creating records in migrations via the class itself, but I’m not clear how to populate the uuid field easily if I do that.

But, currently, the issue remains that an old migration is breaking.

Here is what happened.

I added a new column ‘sync_enabled’. In my base model, I now have this:

    public function rules() {
        return [
            [['sync_enabled'], 'boolean'],
            [['uuid'], 'string', 'max' => 36],
            [['name', 'name_field'], 'string', 'max' => 255],
            [['name'], 'unique'],

When I do a migrate/fresh, I now get this:

Exception 'yii\base\UnknownPropertyException' with message 'Getting unknown property: common\models\base\PsaEntityType::sync_enabled'

in .../vendor/yiisoft/yii2/base/Component.php:155

If I delete the “sync_enabled boolean” rule in the base model, the migrate/fresh works fine again. So it is definitely related to that new column being there.

The old migration just does a loop like so:

foreach ($list as $item) {
  $model = PsaEntityType::find()->...
  if (! isset($model)) {
    ...  create
    ... add new/unrelated column to this issue
 else {
   $model->newUnrelatedCol = x;

Normally this is caused by cache. Did you cleared cache? especially schema cache?

No. I don’t clear any schema caches in my migration files. That’s expected? I hadn’t seen that in the docs.

Note, this is for migrate/fresh. It’s not just a single migrate/up. Does that have an impact?

Might be. Changing table structure have impact on cache, not sure as i stated earlier. You can try after clearing cache. I had similar issues in the past and normally it’s cache.

1 Like

Ah, okay. Yeah, so this happens on our dev side, even during migrate/fresh. Meaning, a brand new install, app never used. I tried playing around with flushing the schema cache but it had no impact. Ultimately, found that we needed to re-arrange the order of operations in the migrate itself to fix the issue.

Got it solved, thanks for the help!!