Skip Menu |

This queue is for tickets about the DBIx-Class-VirtualColumns CPAN distribution.

Report information
The Basics
Id: 51801
Status: resolved
Priority: 0/
Queue: DBIx-Class-VirtualColumns

People
Owner: Nobody in particular
Requestors: preaction [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: Normal
Broken in: 1.01
Fixed in: (no value)



Subject: No virtual columns defined causes error in create
It seems that if VirtualColumns is loaded but no virtual columns are defined, create() gives the error: Can't use an undefined value as a HASH reference at /usr/lib/perl5/site_perl/5.10.1/DBIx/Class/VirtualColumns.pm line 191. By changing line 191 from: if (exists $self->_virtual_columns->{$attr}) { to: if (defined $self->_virtual_columns && exists $self->_virtual_columns->{$attr}) { this fixes the problem and allows the object to be created. I load VirtualColumns in my base Model class (a DBIx::Class) and add columns based on Moose attributes (which may be database columns or virtual columns).
Sorry, that was not the correct fix, this is: sub _virtual_filter { my ($self,$attrs) = @_; if ( !$self->_virtual_columns ) { $self->_virtual_columns( {} ); } my $virtual_attrs = {}; my $main_attrs = {}; foreach my $attr (keys %$attrs) { if (exists $self->_virtual_columns->{$attr}) { $virtual_attrs->{$attr} = $attrs->{$attr}; } else { $main_attrs->{$attr} = $attrs->{$attr}; } } return ($virtual_attrs,$main_attrs); }
From: doug [...] plainblack.com
Okay, so that fixes my problem, but writing a test for what I believed the cause was isn't working. The real problem is because a relationship has loaded VirtualColumns but does not have a VirtualColumn defined. Attached a patch with fix and test
diff -ruN --exclude='.*' --exclude='*.db' DBIx-Class-VirtualColumns-1.01/lib/DBIx/Class/VirtualColumns.pm DBIx-Class-VirtualColumns-1.01.new/lib/DBIx/Class/VirtualColumns.pm --- DBIx-Class-VirtualColumns-1.01/lib/DBIx/Class/VirtualColumns.pm 2008-05-22 12:15:49.000000000 -0500 +++ DBIx-Class-VirtualColumns-1.01.new/lib/DBIx/Class/VirtualColumns.pm 2009-11-21 22:28:45.000000000 -0600 @@ -184,6 +184,10 @@ sub _virtual_filter { my ($self,$attrs) = @_; + + if ( !$self->_virtual_columns ) { + $self->_virtual_columns( {} ); + } my $virtual_attrs = {}; my $main_attrs = {}; diff -ruN --exclude='.*' --exclude='*.db' DBIx-Class-VirtualColumns-1.01/t/006_relation.t DBIx-Class-VirtualColumns-1.01.new/t/006_relation.t --- DBIx-Class-VirtualColumns-1.01/t/006_relation.t 1969-12-31 18:00:00.000000000 -0600 +++ DBIx-Class-VirtualColumns-1.01.new/t/006_relation.t 2009-11-21 22:34:19.000000000 -0600 @@ -0,0 +1,39 @@ +# -*- perl -*- + +# t/006_relation.t -- Test relationship stuff + +use Class::C3; +use strict; +use Test::More; +use warnings; +no warnings qw(once); + +BEGIN { + eval "use DBD::SQLite"; + plan $@ + ? ( skip_all => 'needs DBD::SQLite for testing' ) + : ( tests => 3 ); +} + +use lib qw(t/lib); + +use_ok( 'VCTest' ); + +use_ok( 'VCTest::Schema' ); + +my $schema = VCTest->init_schema(); + +# Make sure we can still get a Test2 w/o adding a virtual column +my $test3; +eval { $test3 = $schema->resultset('Test2')->create({ + id => "12", + name => "nayme", + test3 => { + id => "12", + name => "other nayme", + }, +}); }; +ok( !$@, 'Create row with relation that has loaded VC but has no VCs defined' ) +or diag ( $@ ); + + diff -ruN --exclude='.*' --exclude='*.db' DBIx-Class-VirtualColumns-1.01/t/lib/VCTest/Schema/Test2.pm DBIx-Class-VirtualColumns-1.01.new/t/lib/VCTest/Schema/Test2.pm --- DBIx-Class-VirtualColumns-1.01/t/lib/VCTest/Schema/Test2.pm 2008-05-21 08:55:19.000000000 -0500 +++ DBIx-Class-VirtualColumns-1.01.new/t/lib/VCTest/Schema/Test2.pm 2009-11-21 22:26:19.000000000 -0600 @@ -23,5 +23,6 @@ }, ); __PACKAGE__->set_primary_key('id'); - -1; \ No newline at end of file +__PACKAGE__->might_have( test3 => 'VCTest::Schema::Test3', 'id' ); + +1; diff -ruN --exclude='.*' --exclude='*.db' DBIx-Class-VirtualColumns-1.01/t/lib/VCTest/Schema/Test3.pm DBIx-Class-VirtualColumns-1.01.new/t/lib/VCTest/Schema/Test3.pm --- DBIx-Class-VirtualColumns-1.01/t/lib/VCTest/Schema/Test3.pm 1969-12-31 18:00:00.000000000 -0600 +++ DBIx-Class-VirtualColumns-1.01.new/t/lib/VCTest/Schema/Test3.pm 2009-11-21 22:23:07.000000000 -0600 @@ -0,0 +1,22 @@ +package # Hide from PAUSE + VCTest::Schema::Test3; + +use base 'DBIx::Class'; + +__PACKAGE__->load_components(qw/VirtualColumns PK::Auto Core/); +__PACKAGE__->table("test3"); +__PACKAGE__->add_columns( + "id", + { + data_type => "integer", + is_nullable => 0, + }, + "name", + { + data_type => "varchar", + is_nullable => 0, + }, +); +__PACKAGE__->set_primary_key('id'); + +1; diff -ruN --exclude='.*' --exclude='*.db' DBIx-Class-VirtualColumns-1.01/t/lib/VCTest/Schema.pm DBIx-Class-VirtualColumns-1.01.new/t/lib/VCTest/Schema.pm --- DBIx-Class-VirtualColumns-1.01/t/lib/VCTest/Schema.pm 2008-05-21 08:23:54.000000000 -0500 +++ DBIx-Class-VirtualColumns-1.01.new/t/lib/VCTest/Schema.pm 2009-11-21 22:24:40.000000000 -0600 @@ -3,6 +3,6 @@ use base qw/DBIx::Class::Schema/; -__PACKAGE__->load_classes(qw/Test1 Test2/); +__PACKAGE__->load_classes(qw/Test1 Test2 Test3/); -1; \ No newline at end of file +1; diff -ruN --exclude='.*' --exclude='*.db' DBIx-Class-VirtualColumns-1.01/t/var/vctest.sql DBIx-Class-VirtualColumns-1.01.new/t/var/vctest.sql --- DBIx-Class-VirtualColumns-1.01/t/var/vctest.sql 2008-05-21 09:54:58.000000000 -0500 +++ DBIx-Class-VirtualColumns-1.01.new/t/var/vctest.sql 2009-11-21 22:25:57.000000000 -0600 @@ -2,6 +2,7 @@ DROP TABLE IF EXISTS test1; DROP TABLE IF EXISTS test2; +DROP TABLE IF EXISTS test3; CREATE TABLE test1 ( id INTEGER PRIMARY KEY AUTOINCREMENT, @@ -15,4 +16,9 @@ description TEXT ); +CREATE TABLE test3 ( + id INTEGER PRIMARY KEY, + name TEXT NOT NULL +); + COMMIT;
Thank you for your bug report and patch. http://search.cpan.org/~maros/DBIx-Class-VirtualColumns-1.02/ On Sa. 21. Nov. 2009, 23:36:29, preaction wrote: Show quoted text
> Okay, so that fixes my problem, but writing a test for what I believed > the cause was isn't > working. > > The real problem is because a relationship has loaded VirtualColumns > but does not have a > VirtualColumn defined. > > Attached a patch with fix and test