[Dbix-class] Default values

Dan Boger dan at peeron.com
Mon Apr 30 22:05:00 GMT 2007


Hi,

I've run into a weird problem.  I'm trying to set default values in a
new row, as described here:

  http://search.cpan.org/~blblack/DBIx-Class-0.07006/lib/DBIx/Class/Manual/Cookbook.pod#Setting_default_values_for_a_row

The error I'm seeing:

    apocalypse$  prove -v t/Peeron2/ActivityLog.t
    t/Peeron2/ActivityLog....ok 1 - Old log test entries not found
    Can't use string ("Peeron2::Schema::ActivityLog") as a HASH ref while "strict refs" in use at /usr/lib/perl5/site_perl/5.8.5/DBIx/Class/Row.pm line 97.
    1..1
    # Looks like your test died just after 1.


My test:

    #!/usr/bin/perl -w

    use Test::More qw/no_plan/; 
    use lib qw#/usr/home/peeron/lib/modules#;
    use Peeron2::Schema;
    use strict;

    my %sample = (itemtype => "other",
                  text     => "testing P2S:ActivityLog - $$");

    my $rs = Peeron2::Schema->resultset("ActivityLog");
    is($rs->search({text => $sample{text}})->count, 0, "Old log test entries not found");

    # create with minimal data
    my $orig = $rs->create({%sample});
    ok($orig, "New line was added using defaults");

    my $lines = $rs->search({text => $sample{text}});
    is($lines->count, 1, "The new line is in the database");


And my class definition:

    package Peeron2::Schema::ActivityLog;
    use strict;
    use warnings;
    use base 'DBIx::Class';

    __PACKAGE__->load_components("PK::Auto", "Core");
    __PACKAGE__->table("ActivityLog");
    __PACKAGE__->add_columns(
      "logid", 
      { data_type => "MEDIUMINT", default_value => undef, is_nullable => 0, size => 8, },
      "date",
      { data_type => "DATE", is_nullable => 0, size => 10, },
      "unixtime",
      { data_type => "INT", is_nullable => 0, size => 10 },
      "itemtype",
      { data_type => "ENUM", default_value => "other", is_nullable => 0, size => 8 },
      "itemid",
      { data_type => "VARCHAR", default_value => undef, is_nullable => 1, size => 16, },
      "text",
      { data_type => "VARCHAR", default_value => "", is_nullable => 0, size => 255 },
      "url",
      { data_type => "VARCHAR", default_value => undef, is_nullable => 1, size => 255, },
      "userid",
      { data_type => "SMALLINT", default_value => undef, is_nullable => 1, size => 5, },
    );
    __PACKAGE__->set_primary_key("logid");

    __PACKAGE__->belongs_to(userid => 'Peeron2::Schema::Users');

    sub new {
      my ($self, $attrs) = @_;

      $attrs->{unixtime} = time if not $attrs->{unixtime} or $attrs->{unixtime} =~ /\D/;
      $attrs->{date} = sprintf("%04d-%02d-%02d", (localtime)[5]+1900, (localtime)[4]+1, (localtime)[3])
        unless $attrs->{date} and $attrs->{date} =~ /^\d\d\d\d-\d\d-\d\d$/;

      $self->next::method($attrs);

      return $self;
    }

    1;

Any idea what I'm doing wrong?  Any additional debug info I can get?  If
I remove the 'sub new' entry, these tests pass (without the default
values, of course).

Any suggestions would be greatly appreciated!

Thanks.

Dan

-- 
Dan Boger
dan at peeron.com



More information about the Dbix-class mailing list