Support MySQL default values for blob, text and json field types

Created on 14 September 2021, about 3 years ago
Updated 25 February 2023, over 1 year ago

Problem/Motivation

Over in #3232699: SQLite schema introspection implementation struggles with NULL default values we're fixing some SQLite default value bugs and the fact that MySQL 5.7 doesn't support default values for blob, text and json fields came to light. Drupal has gone through quite a bit of pain over the years with this. See:

The good news is that MySQL changed its handling of default values to allow blob, text or JSON in 8.0.13: https://dev.mysql.com/doc/refman/8.0/en/data-type-defaults.html (thanks @longwave for this info).

The bad news is that according to the docs...

The BLOB, TEXT, GEOMETRY, and JSON data types can be assigned a default value only if the value is written as an expression, even if the expression value is a literal:

This is permitted (literal default specified as expression):

CREATE TABLE t2 (b BLOB DEFAULT ('abc'));

This produces an error (literal default not specified as expression):

CREATE TABLE t2 (b BLOB DEFAULT 'abc');

So we're going to need to fix up \Drupal\Core\Database\Driver\mysql\Schema::createFieldSql() to cope with this.

Steps to reproduce

Do something like...

    // Create the table.
    $table_specification = [
      'description' => 'Test table.',
      'fields' => [
        'column1'  => [
          'type' => 'int',
          'default' => NULL,
        ],
        'column2'  => [
          'type' => 'text',
          'default' => 'a literal value',
        ],
      ],
    ];
    $schema->createTable('test table', $table_specification);

Proposed resolution

Fix up \Drupal\Core\Database\Driver\mysql\Schema::createFieldSql() to cope with this.
Determine what to do about MySQL 5.7. Default values on text fields is supported by the other db engines we support.

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

📌 Task
Status

Active

Version

10.1

Component
MySQL driver 

Last updated 30 days ago

Created by

🇬🇧United Kingdom alexpott 🇪🇺🌍

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.

Production build 0.71.5 2024