Error when executing database migration in Yii2: ExceptionYii\di\NotInstantableExceptionwith messageFailed to instantiate component or class “m220810_154914_create_table_access_tokens”.
1. Error when executing database migration in YII2: ExceptionYii\di\NotInstantableExceptionwith messageFailed to instantiate component or class “m220810_154914_create_table_access_tokens”.. as shown in Figure 1
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 is copied from other projects. The contents of the file are as follows
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. The specific reason has not been found for the time being, and it is decided to create a new migration file, and then copy the migration implementation in the file. Execute the command: ./yii migrate/create create_access_tokens_table. Execute the migration, no errors are reported. The reason will be found later, and the migration can be executed normally after deleting the namespace console\migrations;
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.
