Skip to content

[#17689] - read back the generated id when the identity is 0 on adapters without sequences - #795

Merged
niden merged 1 commit into
phalcon:v6.0.xfrom
nadorator:fix-mysql-zero-identity
Oct 26, 2026
Merged

niden merged 1 commit into
phalcon:v6.0.xfrom
nadorator:fix-mysql-zero-identity

Conversation

@nadorator

Copy link
Copy Markdown
Contributor

Hello!

In raising this pull request, I confirm the following:

  • I have read and understood the Contributing Guidelines
  • I have checked that another pull request for this purpose does not exist
  • I wrote some tests for this PR
  • I have updated the relevant CHANGELOG
  • I have created a PR for the documentation about this change

Small description of change:

Port of phalcon/cphalcon#17690. Model::doLowInsert() reuses any non-empty identity value instead of calling lastInsertId() (fix for #16436). On MySQL, 0 is not an explicit id: MySQL replaces it with the next auto-increment value, so a model whose identity property is 0 keeps 0 after save() / create().

The value on the model is now reused on adapters with sequences (as before) and for any explicit non-zero value. Only 0 on adapters without sequences goes through lastInsertId(), which returns the stored id (the generated one on MySQL, 0 on SQLite). The PostgreSQL behaviour from #16436 is unchanged. supportSequences() is now called once and stored in $useSequences.

Tests (tests/database/Mvc/Model/CreateTest.php), same as in cphalcon:

  • testMvcModelCreateWithZeroIdentityMysql (data sets 0 and '0'): the model gets the id stored in the table.
  • testMvcModelCreateWithZeroIdentitySqlite (data sets 0 and '0'): 0 is stored as given and read back as the string '0'.
  • testMvcModelCreateWithExplicitIdMysqlSqlite: an explicit 77 is kept as int 77.

Checked locally with PHP 8.2.35: CreateTest sqlite OK (5 tests, 18 assertions), mysql/MariaDB 10.6 OK (5 tests, 20 assertions); phpcs and phpstan pass on the changed files.

Thanks

🤖 Generated with Claude Code

…ers without sequences

Assisted-by: Claude Code
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@niden
niden merged commit 2b992db into phalcon:v6.0.x Oct 26, 2026
31 of 32 checks passed
@niden

niden commented Oct 26, 2026

Copy link
Copy Markdown
Member

Thank you @nadorator

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.

2 participants