Invalid datetime format for empty field value for int not null = false field schema

Created on 14 August 2015, over 9 years ago
Updated 7 November 2024, 14 days ago

Problem/Motivation

Steps to reproduce.

  1. Create a field with an int, not null = false schema definition.
  2. Do not override isEmpty() from FieldItemBase.
  3. Make sure the field instance is non-required.

This results in a bit of a mismatch. The DB schema requires a value, the default isEmpty() doesn't flag empty string as empty. Resulting in a NULL value being passed to the DB. Then we create terrible UX by providing a really misleading SQL error:
Drupal\Core\Entity\EntityStorageException: SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect integer value: '' for column 'field_progress_value' at row 2: INSERT INTO {node__field_progress}

Example schema:

  public static function schema(FieldStorageDefinitionInterface $field_definition) {
    return array(
      'columns' => array(
        'value' => array(
          'type' => 'int',
          'not null' => FALSE,
        ),
      ),
    );
  }

Proposed resolution

Investigate why the datatime error is getting throw and return a more appropriate exception.

Remaining tasks

User interface changes

API changes

Data model changes

Beta phase evaluation

<!--Uncomment the relevant rows for the issue. -->
🐛 Bug report
Status

Closed: outdated

Version

11.0 🔥

Component

field system

Created by

heddn Nicaragua

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇳🇿New Zealand quietone

    I tested this today on a fresh install of Drupal 11.x, using drush to generate the field type. I was not able to reproduce the problem.

    Since this was reported 10 years ago and there has been just one confirmation of the problem 8 years ago, I am closing this as outdated.

    If you are experiencing this problem on a supported version of Drupal reopen the issue, by setting the status to 'Active', and provide complete steps to reproduce the issue (starting from "Install Drupal core").

    Thanks!

Production build 0.71.5 2024