Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Doctrine fails to apply DEFAULT CURRENT_TIMESTAMP for DateTime field #11702

Open
Legend999 opened this issue Oct 27, 2024 · 0 comments
Open

Doctrine fails to apply DEFAULT CURRENT_TIMESTAMP for DateTime field #11702

Legend999 opened this issue Oct 27, 2024 · 0 comments

Comments

@Legend999
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant