{"record":{"id":"7801d41076aac593","repo":"yiisoft/yii2","slug":"you-should-configure-cache-component-to-use-data","errorCode":null,"errorMessage":"You should configure \"cache\" component to use database before executing this migration.","messagePattern":"You should configure \"cache\" component to use database before executing this migration\\.","errorType":"exception","errorClass":"yii\\base\\InvalidConfigException","httpStatus":null,"severity":"error","filePath":"framework/caching/migrations/m150909_153426_cache_init.php","lineNumber":29,"sourceCode":"use yii\\db\\Migration;\n\n/**\n * Initializes Cache tables.\n *\n * @author Misbahul D Munir <misbahuldmunir@gmail.com>\n * @since 2.0.7\n */\nclass m150909_153426_cache_init extends Migration\n{\n    /**\n     * @throws yii\\base\\InvalidConfigException\n     * @return DbCache\n     */\n    protected function getCache()\n    {\n        $cache = Yii::$app->getCache();\n        if (!$cache instanceof DbCache) {\n            throw new InvalidConfigException('You should configure \"cache\" component to use database before executing this migration.');\n        }\n\n        return $cache;\n    }\n\n    /**\n     * {@inheritdoc}\n     */\n    public function up()\n    {\n        $cache = $this->getCache();\n        $this->db = $cache->db;\n\n        $tableOptions = null;\n        if ($this->db->driverName === 'mysql') {\n            // https://stackoverflow.com/questions/766809/whats-the-difference-between-utf8-general-ci-and-utf8-unicode-ci\n            $tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB';\n        }","sourceCodeStart":11,"sourceCodeEnd":47,"githubUrl":"https://github.com/yiisoft/yii2/blob/66f00d18a29b520f85e8e8f1e32d1e7e7b556cac/framework/caching/migrations/m150909_153426_cache_init.php#L11-L47","documentation":"Thrown by the framework cache migration m150909_153426_cache_init when Yii::$app->getCache() does not return a yii\\caching\\DbCache instance. The migration creates the table DbCache stores its data in, so it insists that the application's cache component (in the configuration the migration runs under, normally console config) already points at a database-backed cache. Any other cache class (FileCache, RedisCache, dummy default) is treated as running the migration against the wrong setup.","triggerScenarios":"Running php yii migrate --migrationPath=@yii/caching/migrations while the console application's 'cache' component is FileCache or MisCache/MemCache; cache not configured at all in console config (web-only component); a project where web config uses DbCache but config/console.php still has the default; CI running migrations with a minimal config that omits the cache component.","commonSituations":"First-time setup of DbCache following the guide: developer added DbCache to web config but migrations run under console config; environments with split web/console configs drifting apart; automated deploy pipelines that use a stripped-down console config for migrate; adding the migration path to the application's migrationNamespaces without completing the cache switch everywhere.","solutions":["Configure the console application's cache as DbCache before running the migration: in config/console.php set 'cache' => ['class' => \\yii\\caching\\DbCache::class, 'db' => 'db' (or a dedicated cache connection).","Verify with php yii (help/inspect) or a tinker script that Yii::$app->cache is DbCache in the CLI context, then re-run the migration.","If you do not intend to use DbCache, remove the @yii/caching/migrations path from the migrate command — this migration is only needed for database-backed cache."],"exampleFix":"// before: config/console.php has no/other cache component\n// $ php yii migrate --migrationPath=@yii/caching/migrations\n// -> 'You should configure \"cache\" component to use database before executing this migration.'\n\n// after: config/console.php\n'components' => [\n    'cache' => [\n        'class' => \\yii\\caching\\DbCache::class,\n        'db' => 'db',\n    ],\n],\n// $ php yii migrate --migrationPath=@yii/caching/migrations  // now creates the cache table","handlingStrategy":"validation","validationCode":"// Pre-flight before running the cache migrations (console context)\nif (!(Yii::$app->get('cache', false) instanceof \\yii\\caching\\DbCache)) {\n    throw new \\yii\\console\\Exception(\n        'Configure components.cache as yii\\caching\\DbCache (console config) before running caching migrations.'\n    );\n}","typeGuard":"function isDbCache($component): bool\n{\n    return $component instanceof \\yii\\caching\\DbCache;\n}","tryCatchPattern":null,"preventionTips":["Configure DbCache in config/console.php before running @yii/caching/migrations — migrations execute under console config","Keep web and console cache configuration in a shared config file to prevent drift","Only register the caching migrations path when the project actually uses DbCache"],"tags":["php","yii2","cache","migration","dbcache","console","configuration"],"backgroundTag":"migration-config-mismatch","analyzedSha":"66f00d18a29b520f85e8e8f1e32d1e7e7b556cac","analyzedAt":"2026-08-17T05:17:23.470Z","schemaVersion":2},"datasetVersion":"2026-08-17T09:17:11.063Z"}