Skip to content

Conversation

@JanJakes
Copy link
Member

@JanJakes JanJakes commented Nov 6, 2025

In MySQL non-STRICT mode, when inserting from a SELECT query:

When a column is declared as NOT NULL, inserting a NULL value saves an IMPLICIT DEFAULT value instead. This behavior only applies to the INSERT ... SELECT syntax (not VALUES or SET).

In other words:

CREATE TABLE t (value INT NOT NULL);

-- Strict mode:
INSERT INTO t VALUES (NULL); -- error
INSERT INTO t SET value = NULL; -- error
INSERT INTO t SELECT NULL; -- error
INSERT INTO t VALUES ((SELECT NULL)); -- error

-- Non-strict mode:
INSERT INTO t VALUES (NULL); -- error
INSERT INTO t SET value = NULL; -- error
INSERT INTO t SELECT NULL; -- OK, saves 0 (the implicit default for integer)
INSERT INTO t VALUES ((SELECT NULL)); -- error

This fixes some failures in the activation of the top 100 plugins.

@JanJakes JanJakes force-pushed the fix-insert-from-select branch from 6df418e to eddd736 Compare November 6, 2025 14:59
@JanJakes JanJakes requested a review from a team November 6, 2025 15:16
@adamziel
Copy link
Collaborator

adamziel commented Nov 6, 2025

How are you even coming up with these nuances. 🤯

if ( ! $is_strict_mode && $is_insert_from_select && 'NO' === $column['IS_NULLABLE'] ) {
$implicit_default = self::DATA_TYPE_IMPLICIT_DEFAULT_MAP[ $column['DATA_TYPE'] ] ?? null;
if ( null !== $implicit_default ) {
$value = sprintf( 'COALESCE(%s, %s)', $value, $this->connection->quote( $implicit_default ) );
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is that SQLite coalesce? Will it give us the result we need?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@adamziel Yes. There is a very similar logic for UPDATE SET ... = NULL already:

/*
* In MySQL non-STRICT mode, when a column is declared as NOT NULL,
* updating to a NULL value saves an IMPLICIT DEFAULT value instead.
* This behavior does not apply to ON DUPLICATE KEY UPDATE clauses.
*/
$is_on_duplicate_key_update = 'insertUpdateList' === $parent_node->rule_name;
if ( ! $is_strict_mode && ! $is_nullable && ! $is_on_duplicate_key_update ) {
$implicit_default = self::DATA_TYPE_IMPLICIT_DEFAULT_MAP[ $data_type ] ?? null;
if ( null !== $implicit_default ) {
$value = sprintf( 'COALESCE(%s, %s)', $value, $this->connection->quote( $implicit_default ) );
}
}

@JanJakes
Copy link
Member Author

JanJakes commented Nov 6, 2025

How are you even coming up with these nuances. 🤯

@adamziel Going through the few remaining errors from the top 100 plugins 🙂

@adamziel adamziel merged commit 2c973d4 into develop Nov 6, 2025
14 checks passed
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

Successfully merging this pull request may close these issues.

3 participants