'Doctrine default value CURRENT_TIMESTAMP for a column
I am trying to create an entity with created_at field that has default value CURRENT_TIMESTAMP.
But when I try to persist an entity to a database I get this error:
An exception occurred while executing a query: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'created_at' cannot be null
It looks like doctrine tries to send NULL value explicitly for that column instead of not sending it.
Is it possible to have default value CURRENT_TIMESTAMP for a field that generated on the DBMS side, not by any hooks of the entity or lifecycle callbacks?
Here is my xml mapping:
<?xml version="1.0" encoding="utf-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping https://www.doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
<entity name="App\Entity\BusinessTrip" table="business_trip">
<id name="id" type="binary" column="id">
<generator strategy="IDENTITY"/>
</id>
<field name="createdAt" type="datetime" column="created_at" nullable="false">
<options>
<option name="default">CURRENT_TIMESTAMP</option>
</options>
</field>
<field name="modifiedAt" type="datetime" column="modified_at" nullable="true"/>
<field name="deletedAt" type="datetime" column="deleted_at" nullable="true"/>
<field name="createdBy" type="binary" column="created_by" nullable="false"/>
<field name="dateFrom" type="date" column="date_from" nullable="false"/>
<field name="dateTo" type="date" column="date_to" nullable="false"/>
</entity>
</doctrine-mapping>
Code of the entity is pretty long, so I put only declaration of the field here:
class BusinessTrip implements JsonSerializable
{
private ?DateTimeInterface $createdAt;
At the point of persisting this entity that field is NULL.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
