{"record":{"id":"07682fa604a6f491","repo":"doctrine/orm","slug":"unexpected-number-of-database-columns","errorCode":null,"errorMessage":"Unexpected number of database columns.","messagePattern":"Unexpected number of database columns\\.","errorType":"exception","errorClass":"LengthException","httpStatus":null,"severity":"error","filePath":"src/Persisters/Entity/BasicEntityPersister.php","lineNumber":337,"sourceCode":"            . ' FROM ' . $tableName\n            . ' WHERE ' . implode(' = ? AND ', $identifier) . ' = ?';\n\n        $flatId = $this->identifierFlattener->flattenIdentifier($versionedClass, $id);\n\n        $values = $this->conn->fetchNumeric(\n            $sql,\n            array_values($flatId),\n            $this->extractIdentifierTypes($id, $versionedClass),\n        );\n\n        if ($values === false) {\n            throw new LengthException('Unexpected empty result for database query.');\n        }\n\n        $values = array_combine(array_keys($columnNames), $values);\n\n        if (! $values) {\n            throw new LengthException('Unexpected number of database columns.');\n        }\n\n        return $values;\n    }\n\n    /**\n     * @param mixed[] $id\n     *\n     * @return list<ParameterType|int|string>\n     * @phpstan-return list<ParameterType::*|ArrayParameterType::*|string>\n     */\n    final protected function extractIdentifierTypes(array $id, ClassMetadata $versionedClass): array\n    {\n        $types = [];\n\n        foreach ($id as $field => $value) {\n            $types = [...$types, ...PersisterHelper::inferParameterTypes($field, $value, $versionedClass, $this->em)];\n        }","sourceCodeStart":319,"sourceCodeEnd":355,"githubUrl":"https://github.com/doctrine/orm/blob/d9b9ff73016bf598ae07515f97289ce8074e97a5/src/Persisters/Entity/BasicEntityPersister.php#L319-L355","documentation":"In fetchVersionAndNotUpsertableValues(), after the re-select succeeds, array_combine() pairs the expected column keys with the returned row. An empty/false result means the number of columns the ORM expects (generated + version field columns it collected from fieldMappings) does not match what the query returned — an internal mapping invariant break, e.g., a versionField or generated attribute pointing at a field that is not a real mapped column.","triggerScenarios":"ClassMetadata marked isVersioned with a versionField not present (or misnamed) in fieldMappings; a `generated` declaration on a field the persister did not select; SELECT list built empty so the combine produces nothing.","commonSituations":"Hand-edited/programmatic ClassMetadata where the version field name drifts from the real field mapping; metadata cached from an older entity definition after renaming a versioned or generated column without clearing the metadata cache.","solutions":["Verify the #[Version] field actually exists as a mapped field column (name and mapping key match)","Clear the metadata cache after changing versioned/generated fields (bin/console cache:clear or delete the query/metadata cache pool)","Reproduce with logging enabled and check the generated SELECT column list against the table schema"],"exampleFix":null,"handlingStrategy":"validation","validationCode":"$meta = $em->getClassMetadata($entity::class);\nif ($meta->isVersioned && ! isset($meta->fieldMappings[$meta->versionField])) {\n    throw new RuntimeException('Version field is not a mapped field; fix metadata and clear cache.');\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Keep #[Version] on real mapped columns","Clear metadata cache after renaming versioned/generated fields","Validate programmatically built ClassMetadata with schema-tool validate"],"tags":["orm-internal","version-field","generated-columns","metadata-cache"],"backgroundTag":"orm-metadata-inconsistency","analyzedSha":"d9b9ff73016bf598ae07515f97289ce8074e97a5","analyzedAt":"2026-08-21T06:13:15.863Z","schemaVersion":2},"datasetVersion":"2026-08-21T11:28:35.574Z"}