Add support for NO_AUTO_VALUE_ON_ZERO SQL mode#366
Open
Conversation
By default, MySQL treats a literal 0 in an AUTO_INCREMENT column the
same as NULL and generates the next sequence value. This behavior is
suppressed by the NO_AUTO_VALUE_ON_ZERO SQL mode, which is not part of
the default modes.
To emulate this on SQLite, rewrite the value to NULL via
NULLIF(CAST(... AS INTEGER), 0). The explicit CAST is required because
SQLite compares storage classes strictly, so NULLIF('0', 0) returns
'0', not NULL — and WordPress 1.0 emits the string form.
Also exclude AUTO_INCREMENT columns from the non-strict IMPLICIT
DEFAULT COALESCE wrapping, so a NULL (original or rewritten from 0)
always advances the sequence.
See: https://dev.mysql.com/doc/refman/8.4/en/sql-mode.html#sqlmode_no_auto_value_on_zero
NO_AUTO_VALUE_ON_ZERO SQL mode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Emulates MySQL's
NO_AUTO_VALUE_ON_ZEROSQL mode behavior on SQLite.MySQL treats a literal
0in anAUTO_INCREMENTcolumn the same asNULLand generates the next sequence value. This can be suppressed by theNO_AUTO_VALUE_ON_ZEROSQL mode, which is not part of MySQL's default modes.Some legacy WordPress versions rely on the default behavior — e.g. WP 1.0's
post.phpemits:On MySQL this stores
ID = 1. The current SQLite driver storedID = 0, which fails with legacy WP.Scope
INSERT/REPLACEvalue lists (includingVALUES,SET, andSELECTforms).UPDATE(and the UPDATE half ofINSERT ... ON DUPLICATE KEY UPDATE). MySQL never auto-generatesAUTO_INCREMENTvalues on UPDATE — zeros are stored literally.The default
sql_modewas already correct —NO_AUTO_VALUE_ON_ZEROis absent, matching MySQL 8.0 defaults.Tests
testDefaultSqlModeDoesNotIncludeNoAutoValueOnZero— asserts the default modes.testAutoIncrementZeroAdvancesSequenceByDefault—0,'0', andNULLall generate new ids.testAutoIncrementZeroAdvancesSequenceForAllInsertShapes— coversINSERT ... SET,INSERT ... SELECT, andREPLACE.testNoAutoValueOnZeroSqlMode— with the mode on, literal0is stored as-is; onlyNULLadvances the sequence.See: https://dev.mysql.com/doc/refman/8.4/en/sql-mode.html#sqlmode_no_auto_value_on_zero