没有不值得去解决的问题,也没有不值得去学习的技术!

Database Migration Error in Yii2: Exception ‘yii\di\NotInstantiableException’ with message ‘Failed to instantiate component or class “m220810_154914_create_table_access_tokens”.’

在 Yii2 中执行数据库迁移时报错:Exception 'yii\di\NotInstantiableException' with message 'Failed to instantiate component or class "m220810_154914_create_table_access_tokens".'

1. When running a database migration in Yii2, an error occurred: Exception ‘yii\di\NotInstantiableException’ with message ‘Failed to instantiate component or class “m220810_154914_create_table_access_tokens”.’ See Figure 1.

Error when running a database migration in Yii2: Exception 'yii\di\NotInstantiableException' with message 'Failed to instantiate component or class "m220810_154914_create_table_access_tokens".'
Figure 1
Plaintext
PS C:\wwwroot\object\src> ./yii migrate
Yii Migration Tool (based on Yii v2.0.49.4)

Total 1 new migration to be applied:
        m220810_154914_create_table_access_tokens

Apply the above migration? (yes|no) [no]:yes
*** applying m220810_154914_create_table_access_tokens
Exception 'yii\di\NotInstantiableException' with message 'Failed to instantiate component or class "m220810_154914_create_table_access_tokens".'

in C:\wwwroot\object\src\vendor\yiisoft\yii2\di\Container.php:509

Caused by: Exception 'ReflectionException' with message 'Class m220810_154914_create_table_access_tokens does not exist'

in C:\wwwroot\object\src\vendor\yiisoft\yii2\di\Container.php:507

Stack trace:
#0 C:\wwwroot\object\src\vendor\yiisoft\yii2\di\Container.php(507): ReflectionClass->__construct('m220810_154914_...')
#1 C:\wwwroot\object\src\vendor\yiisoft\yii2\di\Container.php(385): yii\di\Container->getDependencies('m220810_154914_...')
#2 C:\wwwroot\object\src\vendor\yiisoft\yii2\di\Container.php(170): yii\di\Container->build('m220810_154914_...', Array, Array)
#3 C:\wwwroot\object\src\vendor\yiisoft\yii2\BaseYii.php(365): yii\di\Container->get('m220810_154914_...', Array, Array)
#4 C:\wwwroot\object\src\vendor\yiisoft\yii2\console\controllers\MigrateController.php(202): yii\BaseYii::createObject(Array)
#5 C:\wwwroot\object\src\vendor\yiisoft\yii2\console\controllers\BaseMigrateController.php(757): yii\console\controllers\MigrateController->createMigration('m220810_154914_...')
#6 C:\wwwroot\object\src\vendor\yiisoft\yii2\console\controllers\BaseMigrateController.php(216): yii\console\controllers\BaseMigrateController->migrateUp('m220810_154914_...')
#7 [internal function]: yii\console\controllers\BaseMigrateController->actionUp(0)
#8 C:\wwwroot\object\src\vendor\yiisoft\yii2\base\InlineAction.php(57): call_user_func_array(Array, Array)
#9 C:\wwwroot\object\src\vendor\yiisoft\yii2\base\Controller.php(178): yii\base\InlineAction->runWithParams(Array)
#10 C:\wwwroot\object\src\vendor\yiisoft\yii2\console\Controller.php(180): yii\base\Controller->runAction('', Array)
#11 C:\wwwroot\object\src\vendor\yiisoft\yii2\base\Module.php(552): yii\console\Controller->runAction('', Array)
#12 C:\wwwroot\object\src\vendor\yiisoft\yii2\console\Application.php(180): yii\base\Module->runAction('migrate', Array)
#13 C:\wwwroot\object\src\vendor\yiisoft\yii2\console\Application.php(147): yii\console\Application->runAction('migrate', Array)
#14 C:\wwwroot\object\src\vendor\yiisoft\yii2\base\Application.php(384): yii\console\Application->handleRequest(Object(yii\console\Request))
#15 C:\wwwroot\object\src\yii(23): yii\base\Application->run()
#16 {main}

2. This migration file was copied from another project. The file contents are as follows:

PHP
createTable('access_tokens', [
            'id' => $this->string(20),
            'user_id' => $this->string(20),
            'access_token' => $this->string(200)->unique(),
            'ip' => $this->string(128)->defaultValue(''),

            'created_at' => $this->dateTime(),
            'updated_at' => $this->dateTime(),
        ], 'row_format=DYNAMIC');
        $this->addPrimaryKey('pk-id', 'access_tokens', 'id');
        $this->createIndex('idx-uid', 'access_tokens', 'user_id');

    }

    /**
     * {@inheritdoc}
     */
    public function safeDown()
    {
        echo "m220810_154914_create_table_access_tokens cannot be reverted.\n";

        return false;
    }

    /*
    // Use up()/down() to run migration code without a transaction.
    public function up()
    {

    }

    public function down()
    {
        echo "m220810_154914_create_table_access_tokens cannot be reverted.\n";

        return false;
    }
    */
}


3. I could not find the specific cause at the moment, so I decided to create a new migration file and copy the migration implementation from the original file into it. I ran the command: ./yii migrate/create create_access_tokens_table. Running the migration produced no errors. I later found the actual cause: removing the namespace console\migrations; declaration allows the migration to execute normally.

Plaintext
PS C:\wwwroot\object\src> ./yii migrate/create create_access_tokens_table
Yii Migration Tool (based on Yii v2.0.49.4)

Create new migration 'C:\wwwroot\object\src\console/migrations\m250515_031604_create_access_tokens_table.php'? (yes|no) [no]:yes
New migration created successfully.
PS C:\wwwroot\object\src> ./yii migrate
Yii Migration Tool (based on Yii v2.0.49.4)

Total 1 new migration to be applied:
        m250515_031604_create_access_tokens_table

Apply the above migration? (yes|no) [no]:yes
*** applying m250515_031604_create_access_tokens_table
    > create table access_tokens ... done (time: 0.028s)
    > add primary key pk-id on access_tokens (id) ... done (time: 0.044s)
    > create index idx-uid on access_tokens (user_id) ... done (time: 0.038s)
*** applied m250515_031604_create_access_tokens_table (time: 0.122s)


1 migration was applied.

Migrated up successfully.

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系