Symfony doctrine:schema:update not working
When you run doctrine:schema:update in a Symfony application, especially within an older stack like version 2.3, it is easy to assume the tool is perfectly synced with your entity definitions. However, discrepancies between your code annotations and the generated SQL often stem from metadata caching or specific Doctrine ORM behaviors rather than actual bugs in the schema manager itself.
Consider a scenario where you have a Symfony 2.3 application using Sonata User and have created a custom bundle with an entity named ConfigApp. Initially, the schema syncs without issues. Later, you decide to modify your entityâperhaps increasing string lengths by one character to avoid edge cases in data storage. You update the annotation for the nom field from length=100 to length=101. Despite these changes, running php app/console doctrine:schema:update --dump-sql reveals that Doctrine still intends to create a column with VARCHAR(100), ignoring your updated definition.
This behavior indicates that Doctrine is not reading the latest metadata from your source files but is instead relying on an outdated snapshot stored in the cache. Even after clearing the application cache and running specific commands like doctrine:cache:clear-metadata and deleting contents from the app/cache folders, the problem persists. This suggests the issue lies deeper than a simple PHP opcode cache or standard cache directory; it points to how Doctrine ORM resolves column metadata during the schema introspection phase.
In Symfony 2.3, the default caching strategy for the metadata can be aggressive. When Doctrine scans entities, it serializes the mapping information into a cache file (usually located in app/cache/dev/doctrine.orm.metadata). If this serialized metadata is not invalidated correctly by the standard cache clear commands, or if the cache directory structure prevents proper regeneration, the schema update command will continue to compare your code against the stale cached version.
To resolve this without immediately resorting to a full database migration, you must ensure the metadata cache is truly bypassed. Instead of relying solely on doctrine:cache:clear-metadata, try running the following sequence explicitly before invoking the schema update:
php app/console doctrine:schema:drop --force --if-exists
php app/console doctrine:schema:create
Alternatively, inspect the actual cache directory structure. In Symfony 2.3, the metadata cache files are often stored under app/cache/dev/doctrine.orm.metadata. Ensure that you are deleting this specific file if it exists, rather than just clearing the generic cache. Sometimes, the doctrine:cache:clear-metadata command attempts to clear a different abstraction layer that does not correspond to the physical file used by the schema updater in older versions.
If the issue persists after manual cache deletion, consider upgrading your Doctrine version or Symfony version. The behavior described is known to be more prevalent in legacy versions of Doctrine where metadata caching was less robust than in modern iterations (Doctrine 2.5+). In newer environments, doctrine:schema:update handles cache invalidation much more reliably.
There is also a nuance regarding unique constraints. If the field has a unique=true constraint, Doctrine must ensure that no existing data violates this new length or constraint logic. While this specific case involves length changes, if there are other modifications to constraints or indexes, Doctrine requires an explicit check. Using the --force flag tells Doctrine to drop and recreate tables regardless of these checks, but it is a destructive operation reserved for environments where downtime is acceptable or data migration scripts handle the transfer safely.
When troubleshooting schema updates in Symfony 2.3, always verify that you are looking at the correct cache directory and not just the application runtime cache. The distinction between app/cache (runtime) and the specific Doctrine metadata cache files is crucial for diagnosing why your entity annotations appear ignored by the database layer.