Mapping datetime database columns to date properties raises ConversionException #6363

Open
opened 2026-01-22 15:31:50 +01:00 by admin · 0 comments
Owner

Originally created by @sGy1980de on GitHub (Dec 12, 2019).

Bug Report

Q A
BC Break no
Version 2.6.4

Summary

I got somewhat surprised by an unexpected mapping issue when i wanted to convert datetime columns to date columns. The model was once created with a datetime property, when only date was needed. I spent quite some time in creating a (for my understanding) zero downtime migration script, which addresses the issue and still keeps the API alive during transition.

Current behavior

Mapping datetime values in the database to properties mapped with type date is currently not possible. This results in a Doctrine\DBAL\Types\ConversionException

/var/www/vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/ConversionException.php in conversionFailedFormat
Could not convert database value "2019-12-03 00:00:00" to Doctrine Type date. Expected format: Y-m-d

How to reproduce

  • define model with datetime mapped property and fill database with some rows
  • change mapping in the model from datetime to date for the property
  • don't apply the appropriate database migration, so the ORM needs to map the old time-indexed values to the now date type mapped property

Expected behavior

The ORM should truncate the time-index from the database value on it's own and only use the date part.

Further notes

Even more surprising to me. The ORM maps date values to datetime mapped properties without issues. So the migration would have worked the other way around with no issues.

Originally created by @sGy1980de on GitHub (Dec 12, 2019). ### Bug Report | Q | A |------------ | ------ | BC Break | no | Version | 2.6.4 #### Summary I got somewhat surprised by an unexpected mapping issue when i wanted to convert `datetime` columns to `date` columns. The model was once created with a `datetime` property, when only `date` was needed. I spent quite some time in creating a (for my understanding) zero downtime migration script, which addresses the issue and still keeps the API alive during transition. #### Current behavior Mapping `datetime` values in the database to properties mapped with type `date` is currently not possible. This results in a `Doctrine\DBAL\Types\ConversionException` ``` /var/www/vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/ConversionException.php in conversionFailedFormat Could not convert database value "2019-12-03 00:00:00" to Doctrine Type date. Expected format: Y-m-d ``` #### How to reproduce - define model with `datetime` mapped property and fill database with some rows - change mapping in the model from `datetime` to `date` for the property - don't apply the appropriate database migration, so the ORM needs to map the old `time-indexed` values to the now `date` type mapped property #### Expected behavior The ORM should truncate the `time-index` from the database value on it's own and only use the `date` part. #### Further notes Even more surprising to me. The ORM maps `date` values to `datetime` mapped properties without issues. So the migration would have worked the other way around with no issues.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#6363