Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bugfix] #58 allow inserts with primary key nulled to be rolled back #57

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/Codeception/Module/Db.php
Original file line number Diff line number Diff line change
Expand Up @@ -808,7 +808,9 @@ private function addInsertedRow(string $table, array $row, $id): void
{
$primaryKey = $this->_getDriver()->getPrimaryKey($table);
$primary = [];
if ($primaryKey !== []) {
if (count($primaryKey) == 1 && is_null($row[$primaryKey[0]])) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not check for "all pk values are null" instead of checking the size is 1 and the single value is null ?

Copy link
Author

@jtopenpetition jtopenpetition Jun 16, 2023

Choose a reason for hiding this comment

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

tbh, I'm actually not certain if auto-increment is even possible in a composite primary key. this might be database specific, again. I would shy away from setting anything, if the primary key had more than one (nulled) column. Someone with broader knowledge than me might know this ...

only size 1 and only if it's null seemed a very safe bet ;o)

addendum: this was also the behaviour before, but ... inverted, if there was an $id, it would be set on the primary key, which - as you pointed out - is actually dangerous, since the last insert id can return a non-primary auto-increment value and would override a proper primary key value.

$primary[$primaryKey[0]] = $id;
} elseif ($primaryKey !== []) {
$filledKeys = array_intersect($primaryKey, array_keys($row));
$missingPrimaryKeyColumns = array_diff_key($primaryKey, $filledKeys);

Expand Down