删除数据库列后出现PDO Exception未知列错误求助
删除数据库废弃列后出现列不存在错误的排查与解决
我在移除数据库中的废弃列后,遇到了如下错误:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'table.my_column' in 'field list'' in /var/www/wap/vendor/cakephp/cakephp/src/Database/Statement/MysqlStatement.php:37
错误由这段代码触发:
$info = $this->Infos->find("InfoContact",["gid" => $info_id])->first();
代码基于Cake\ORM\Query实现,且InfoContact实体里没有引用已删除的列。我全局搜索(Ctrl+Shift+F)没找到该列的显式引用,怀疑是CakePHP的Schema缓存问题,但还没确认。
已尝试的操作
- 项目配置了Schema缓存,尝试将
cacheMetadata设为false,并给_cake_model_缓存前缀添加版本号,错误依旧存在。 - 检查composer.json,确认包含cakephp/migrations,但项目中没有继承AbstractMigration的文件。
app.php中的缓存配置
'Cache' => [ 'default' => [ 'className' => FileEngine::class, 'path' => CACHE, 'url' => env('CACHE_DEFAULT_URL', null), ], /** * Configure the cache used for general framework caching. * Translation cache files are stored with this configuration. * Duration will be set to '+2 minutes' in bootstrap.php when debug = true * If you set 'className' => 'Null' core cache will be disabled. */ '_cake_core_' => [ 'className' => FileEngine::class, 'prefix' => 'myapp_cake_core_', 'path' => CACHE . 'persistent/', 'serialize' => true, 'duration' => '+1 years', 'url' => env('CACHE_CAKECORE_URL', null), ], /** * Configure the cache for model and datasource caches. This cache * configuration is used to store schema descriptions, and table listings * in connections. * Duration will be set to '+2 minutes' in bootstrap.php when debug = true */ '_cake_model_' => [ 'className' => FileEngine::class, 'prefix' => 'myapp_cake_model_', 'path' => CACHE . 'models/', 'serialize' => true, 'duration' => '+1 years', 'url' => env('CACHE_CAKEMODEL_URL', null), ], /** * Configure the cache for routes. The cached routes collection is built the * first time the routes are processed through config/routes.php. * Duration will be set to '+2 seconds' in bootstrap.php when debug = true */ '_cake_routes_' => [ 'className' => FileEngine::class, 'prefix' => 'myapp_cake_routes_', 'path' => CACHE, 'serialize' => true, 'duration' => '+1 years', 'url' => env('CACHE_CAKEROUTES_URL', null), ], 'shortterm' => [ 'className' => FileEngine::class, 'prefix' => 'myapp_short_', 'path' => CACHE . 'views' . DS, 'serialize' => true, 'duration' => '+5 minutes' ] 'Datasources' => [ 'default' => [ 'className' => Connection::class, 'driver' => Mysql::class, 'persistent' => false, 'timezone' => env('APP_DEFAULT_TIMEZONE', 'UTC'), 'flags' => [], 'cacheMetadata' => true, 'log' => false, 'quoteIdentifiers' => true, 'url' => env('DATABASE_URL', null), ], ]
排查与解决建议
- 手动清理缓存文件:因使用FileEngine,直接找到
CACHE/models/目录,删除所有myapp_cake_model_前缀的文件,强制刷新Schema缓存。 - 检查自定义Finder定义:
find("InfoContact")是自定义查询方法,去对应的Table类里查看其实现,确认是否在select()或关联查询中隐式引用了已删除的列。 - 确认缓存配置生效:检查bootstrap.php是否动态修改了
cacheMetadata或缓存时长,即使设为false,旧缓存文件未删除也会继续生效。 - 排查数据库层面依赖:检查该表关联的视图、触发器或存储过程,确认是否有对象引用了已删除的列。
- 开启SQL日志定位:在Datasources配置中将
log设为true,查看实际执行的SQL语句,精准定位哪个部分引用了my_column。
内容的提问来源于stack exchange,提问作者luiza hf
相关产品推荐
相关产品推荐

