Subject: | Unexpected behaviour with TimeStamp component |
DBIx-Class-Migration-0.020 on Ubuntu 10.10
Hi John,
This may actually be an intentional feature but it is not mentioned in
the documentation and it is not intuitive. When using migration scripts
to load demo data into the database (as shown in the tutorial), it
appears that the TimeStamp component is prevented from injecting a
timestamp when a new record is created without explicitly defining a
value for a date/time field that is not nullable and has the
set_on_create attribute.
This may be intentional to ensure the data is in a known state, which
would not be the case if a current timestamp is inserted at creation
time, but then it should be mentioned and ideally a warning should be
issued otherwise it is difficult to find out what is going wrong.
How to reproduce the error:
Using the distribution of Test-DBIx-Class-0.31
in Test-DBIx-Class top level issue:
$ mkdir /share
$ dbic-migration --schema_class Test::DBIx::Class::Example::Schema -Ilib
prepare
$ touch share/migrations/SQLite/deploy/1/002-data.pl
edit 002-data.pl to match the following:
#!/usr/bin/env perl
use strict;
use warnings;
use DBIx::Class::Migration::RunScript;
migrate {
my $self = shift;
$self->schema->resultset('Company')->create({
company_id => '123',
name => 'some_company',
#--------------------------------------------------
# created => '01/04/2012',
#--------------------------------------------------
}
);
};
attempt to install the schema results in an error, desptie the fact that
the created attribute of the Company Result class has the set_on_create
attribute and should have passed:
$ dbic-migration --schema_class Test::DBIx::Class::Example::Schema -Ilib
install
DBIx::Class::ResultSet::create(): DBI Exception: DBD::SQLite::st execute
failed: company.company_id may not be NULL [for Statement "INSERT INTO
company ( name) VALUES ( ? )"] at [..]
uncomment the created line in the script above and installation succeeds.
To verify that creation without passing a value to "created":
$ touch insert_manually.pl
edit the script to match:
#!/usr/bin/env perl
use strict;
use warnings;
use Test::DBIx::Class::Example::Schema;
my $s =
Test::DBIx::Class::Example::Schema->connect("dbi:SQLite:dbname=./share/test-dbix-class-example-schema.db","","");
$s->resultset('Company')->create(
{
company_id => '345',
name => 'some_other_company',
}
);
run it:
$ perl -Ilib insert_manually.pl
passes without error and the data is in the DB:
$ sqlite3 share/test-dbix-class-example-schema.db
Show quoted text
sqlite> select * from company;
123|some_company|01/04/2012
345|some_other_company|2012-04-05 08:59:56
Cheers,
Frank