Doctrine fails to apply DEFAULT CURRENT_TIMESTAMP for DateTime field #7436

Open
opened 2026-01-22 15:51:39 +01:00 by admin · 2 comments
Owner

Originally created by @Legend999 on GitHub (Oct 27, 2024).

Bug Report

Q A
doctrine/orm 3.3.0
doctrine/dbal 4.2.1
database 10.4.28-MariaDB

Summary

Doctrine fails to apply the default timestamp for a field defined with DEFAULT CURRENT_TIMESTAMP. When persisting an entity without manually setting the date, it raises a "column cannot be null" error, despite the database schema having a default value set.

Current behavior

Uncaught PDOException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'sent_at' cannot be null in vendor/doctrine/dbal/src/Driver/PDO/Statement.php:55

Expected behavior

Doctrine should use the default timestamp set in the schema (DEFAULT CURRENT_TIMESTAMP), allowing to create new entities without explicitly setting the value.

How to reproduce

  1. Define a Doctrine entity with a datetime column that has a default value of CURRENT_TIMESTAMP, as shown below:
#[Entity]
#[Table(name: 'messages')]
class Message
{
    #[Column(name: 'sent_at', type: Types::DATETIME_IMMUTABLE, updatable: false, options: ['default' => 'CURRENT_TIMESTAMP'])]
    private readonly DateTimeImmutable $sent_at;
    // ...
}
  1. Generate the schema using orm:schema-tool:create --dump-sql, resulting in SQL similar to:
CREATE TABLE messages (
  id int unsigned AUTO_INCREMENT NOT NULL,
  sent_at datetime DEFAULT CURRENT_TIMESTAMP NOT NULL,
  message text NOT NULL,
  PRIMARY KEY (id)
);
  1. Attempt to create a new Message entity without setting the sent_at property:
$message = new Message($message);
$this->entity_manager->persist($message);
$this->entity_manager->flush();

Additional information

The issue seems to be specific to PHP/Doctrine, as running a raw SQL INSERT INTO messages (message) VALUES ('message') correctly assigns the sent_at field to the current timestamp.

Originally created by @Legend999 on GitHub (Oct 27, 2024). ### Bug Report <!-- Fill in the relevant information below to help triage your issue. --> | Q | A |-------------------------------------------- | ------ | doctrine/orm | 3.3.0 | doctrine/dbal | 4.2.1 | database |10.4.28-MariaDB #### Summary <!-- Provide a summary describing the problem you are experiencing. --> Doctrine fails to apply the default timestamp for a field defined with `DEFAULT CURRENT_TIMESTAMP`. When persisting an entity without manually setting the date, it raises a "column cannot be null" error, despite the database schema having a default value set. #### Current behavior <!-- What is the current (buggy) behavior? --> ``` Uncaught PDOException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'sent_at' cannot be null in vendor/doctrine/dbal/src/Driver/PDO/Statement.php:55 ``` #### Expected behavior <!-- What was the expected (correct) behavior? --> Doctrine should use the default timestamp set in the schema (`DEFAULT CURRENT_TIMESTAMP`), allowing to create new entities without explicitly setting the value. #### How to reproduce <!-- Provide a failing Unit or Functional Test - you can submit one in a Pull Request separately, referencing this bug report. And if you feel like it, why not fix the bug while you're at it? If that is too difficult, provide a link to a minimal repository containing an application that reproduces the bug. If the bug is simple, you may provide a code snippet instead, or even a list of steps. --> 1. Define a Doctrine entity with a datetime column that has a default value of `CURRENT_TIMESTAMP`, as shown below: ``` #[Entity] #[Table(name: 'messages')] class Message { #[Column(name: 'sent_at', type: Types::DATETIME_IMMUTABLE, updatable: false, options: ['default' => 'CURRENT_TIMESTAMP'])] private readonly DateTimeImmutable $sent_at; // ... } ``` 2. Generate the schema using orm:schema-tool:create --dump-sql, resulting in SQL similar to: ``` CREATE TABLE messages ( id int unsigned AUTO_INCREMENT NOT NULL, sent_at datetime DEFAULT CURRENT_TIMESTAMP NOT NULL, message text NOT NULL, PRIMARY KEY (id) ); ``` 3. Attempt to create a new Message entity without setting the sent_at property: ``` $message = new Message($message); $this->entity_manager->persist($message); $this->entity_manager->flush(); ``` # Additional information The issue seems to be specific to PHP/Doctrine, as running a raw SQL `INSERT INTO messages (message) VALUES ('message')` correctly assigns the sent_at field to the current timestamp.
Author
Owner

@goforthanddie commented on GitHub (Dec 9, 2024):

I am experiencing the same problem with ORM 3.3.0.0. However, it helped when I added 'insertable: false' as additional parameter for the column :).

@goforthanddie commented on GitHub (Dec 9, 2024): I am experiencing the same problem with ORM 3.3.0.0. However, it helped when I added 'insertable: false' as additional parameter for the column :).
Author
Owner

@greg0ire commented on GitHub (Nov 20, 2025):

I think this is just a documentation issue, what you've done makes sense @goforthanddie .

@greg0ire commented on GitHub (Nov 20, 2025): I think this is just a documentation issue, what you've done makes sense @goforthanddie .
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#7436