CakePHP ORM and externally restored database: schema cache consistency
0 reputation · 04 Oct 2026, 23:01 UTC
0 reputation · 04 Oct 2026, 23:01 UTC
Goal: determine whether data recovered by an external database backup restore remains consistent when CakePHP ORM queries the restored tables without first invalidating cached table-schema metadata.
CakePHP ORM introspects columns and indexes and caches schema metadata for the request or through configured schema caching. An external restore that changes columns, indexes, or auto-increment state occurs outside the ORM transaction boundary and does not automatically clear that cache. Stale metadata can produce SQL for missing columns, trigger type-casting exceptions, or prevent correct entity hydration. Cache-clearing and table-locator APIs differ across CakePHP 3.x, 4.x, and 5.x, making the invalidation boundary version-sensitive.
Does the ORM detect externally restored structural changes on the next request? Must schema metadata be explicitly cleared before reading restored rows, and does that requirement vary by CakePHP major version?
37775 reputation · 05 Oct 2026, 08:40 UTC
No, the ORM does not detect externally restored structural changes on the next request when schema caching is enabled. You must explicitly clear the schema cache before reading restored rows if the table structure (columns, indexes, types) changed. The requirement to clear the cache does not vary by CakePHP major version, but the command name does: bin/cake orm_cache clear in 3.x versus bin/cake schema_cache clear in 4.x and 5.x.
_cake_model_ cache config. The ORM never compares cached metadata to the live database, so a restore that adds, drops, or renames columns or indexes is invisible until the cache is cleared or expires.describe() runs on first table access each request, so structural changes are picked up automatically with no manual invalidation.| Major | CLI Command | Programmatic Target |
|---|---|---|
| 3.x | bin/cake orm_cache clear | Cache::clear(false, '_cake_model_') |
| 4.x / 5.x | bin/cake schema_cache clear | Cache::clear(false, '_cake_model_') |
Both majors use the same cache config key _cake_model_; only the CLI command was renamed.
cacheMetadata in your datasource config (config/app.php or app_local.php). If it is false, no schema cache clearing is needed.true and the restore changed structure, run the version-appropriate command above or clear the _cake_model_ cache config directly.AUTO_INCREMENT (MySQL) or sequence position (Postgres) against MAX(id) and attempt one insert.What is the value of cacheMetadata for the datasource in your current environment, and which CakePHP major version (3.x, 4.x, or 5.x) are you running? This determines whether clearing is required and which command to use.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.