[Dbix-class] problems with object returned by ->create()
Chisel Wright
chisel at herlpacker.co.uk
Fri Apr 21 12:05:00 CEST 2006
Yesterday I noticed Strange Things while working on a Catalyst
application.
I've managed to boil it down to a reasonably simple test-app (I can
supply the tarball if required, but I prefer not to post attachments to
mailing lists if they aren't required).
I have the following schema:
---- cut here ----
-- we have authentication
CREATE TABLE authentication (
authentication_id SERIAL not null primary key,
username text not null,
password text not null,
authenticated boolean NOT NULL default
False,
UNIQUE (username)
);
-- we have foo
CREATE TABLE foo (
authentication_id SERIAL not null primary key,
username text not null,
password text not null,
authenticated boolean NOT NULL default
False,
UNIQUE (username)
);
---- cut here ----
So a simple authentication table and foo, a copy of the authentication
table.
I also have a couple of test scripts, model_ParleyTest-Authentication.t
and model_ParleyTest-Foo.t. Apart from the table the reference, are
identical:
---- cut here ----
# BEGIN TRANSACTION
AuthTest->model('ParleyTest')->table('authentication')->storage->txn_begin;
# create new record
my $now = scalar(localtime);
my $new_auth =
AuthTest->model('ParleyTest')->table('authentication')->create(
{
username => $now,
password => 'pwd',
}
);
# correct class returned
isa_ok($new_auth, 'AuthTest::Model::ParleyTest::Authentication');
diag Dumper($new_auth->{_column_data});
# new object has values we created it with
is($new_auth->username(), $now, 'username set');
is($new_auth->password(), 'pwd', 'password set');
# now the kicker ... doe we have an id?
ok(defined($new_auth->authentication_id()), 'authentication_id set');
# ROLLBACK TRANSACTION
AuthTest->model('ParleyTest')->table('authentication')->storage->txn_rollback;
---- cut here ----
The big problem I'm having is for 'authentication' the PK column IS NOT
returned in $new_auth, while in the case of 'foo' it is
---- cut here ----
chisel at ferrari AuthTest $ prove --lib --verbose
t/model_ParleyTest-[AF]*t
t/model_ParleyTest-Authentication....1..6
ok 1 - use Catalyst::Test;
ok 2 - use AuthTest::Model::ParleyTest::Authentication;
ok 3 - The object isa AuthTest::Model::ParleyTest::Authentication
# $VAR1 = {
# 'password' => 'pwd',
# 'username' => 'Fri Apr 21 11:01:00 2006'
# };
ok 4 - username set
ok 5 - password set
not ok 6 - authentication_id set
# Failed test 'authentication_id set'
# in t/model_ParleyTest-Authentication.t at line 31.
# Looks like you failed 1 test of 6.
dubious
Test returned status 1 (wstat 256, 0x100)
DIED. FAILED test 6
Failed 1/6 tests, 83.33% okay
t/model_ParleyTest-Foo...............1..6
ok 1 - use Catalyst::Test;
ok 2 - use AuthTest::Model::ParleyTest::Foo;
ok 3 - The object isa AuthTest::Model::ParleyTest::Foo
# $VAR1 = {
# 'authentication_id' => '3',
# 'password' => 'pwd',
# 'username' => 'Fri Apr 21 11:01:02 2006'
# };
ok 4 - username set
ok 5 - password set
ok 6 - authentication_id set
ok
Failed Test Stat Wstat Total Fail Failed List of
Failed
-------------------------------------------------------------------------------
t/model_ParleyTest-Authentication 1 256 6 1 16.67% 6
Failed 1/2 test scripts, 50.00% okay. 1/12 subtests failed, 91.67% okay.
---- cut here ----
Same table structure, same create statement, different results returned.
The lesser issue ... if I test the values of
$new_auth->some_other_table_column() that does exist in the table, but
wasn't one of the columns specified, I get and undefined value - even
for columns with a default value.
I can understand _columns_data potentially not holding this, but surely
if I create a new object I should have access to all the fields in the
new record and not just the ones I specified during creating (and in
most cases the PK columns too).
This is really confusing the hell out of me!
Chisel
--
Chisel Wright
e: chisel at herlpacker.co.uk
w: http://www.herlpacker.co.uk/
This is not an automated signature. I type this in to the bottom of every
message.
More information about the Dbix-class
mailing list