Subject: | Column value bleeds over from previous create call (MSSQL-only?) |
Date: | Thu, 16 Jun 2016 18:25:55 -0500 |
To: | bug-DBIx-Class [...] rt.cpan.org |
From: | Wes Malone <wesdmalone [...] gmail.com> |
I found this one by accident. When creating a row for a result with a
missing 'is_auto_increment' prop in it's ident column, the new row
object receives the ident value from a previous row.
Mildly flaky test follows:
use 5.20.0; use warnings;
use lib 'lib';
use Test::More;
use MyApp::Schema;
my $s = MyApp::Schema->connect($ENV{DBIC_TEST_CONN_STR});
$s->resultset($_)->delete for qw(Artist Cd Track);
my $art = $s->resultset("Artist")->create({ name => 'ZZZ' });
diag 'Artist id: '.$art->id;
my $cd = $art->create_related(cds => { title => 'YYY' });
my $cd_from_db = $s->resultset("Cd")->search({ title => 'YYY' })->next;
diag 'CD id: '.$cd->id;
diag 'Actual CD id: '.$cd_from_db->id;
isnt $cd->id, $art->id, 'created objects id should not come from
the previous object';
my $track = $cd->create_related(tracks => { title => 'XXX' }); # FK error
done_testing;
And the output is
$ perl dbic-test.pl
# Artist id: 15
# CD id: 15
# Actual CD id: 110
not ok 1 - created objects id should not come from the previous object
# Failed test 'created objects id should not come from the
previous object'
# at dbic-test.pl line 16.
# got: '15'
# expected: anything else
DBIx::Class::Storage::DBI::_dbh_execute(): DBI Exception:
DBD::ODBC::st execute failed: [Microsoft][SQL Server Native Client
11.0][SQL Server]The INSERT statement conflicted with the FOREIGN KEY
constraint "track_fk_cdid". The conflict occurred in database
"dbic_test", table "dbo.cd", column 'id'. (SQL-23000) [state was 23000
now 01000]
[Microsoft][SQL Server Native Client 11.0][SQL Server]The
statement has been terminated. (SQL-01000) [for Statement "INSERT INTO
track ( cdid, title) VALUES ( ?, ? )
SELECT SCOPE_IDENTITY()" with ParamValues: 1='15', 2='XXX'] at
dbic-test.pl line 18
Full repro code is here https://github.com/wesQ3/dbic-reused-ident.git
I wasn't able to repro with SQLite, so this may be MSSQL specific.
The provided deploy SQL has the identity seeds for each table
staggered by 100, so failures are easily spotted. If you just started
with blank tables at ID 1, your FK's would match :)
DBIC 0.082821
DBD::ODBC 1.50
SQL Server Native Client 11.0 for Linux
perl 5.20.1