Subject: | delete_related doesn't allow object like create_related does |
I came across this while using a linker table and attempting to use
create_related and delete_related.
When using delete_related, passing an object as the value of the
relation does not work. The object is stringified instead of getting
its primary key. I have attached a test that demonstrates the failure.
I originally found this while using oracle, but it repeats just as well
with sqlite.
Just in case it matters, I found this on perl v5.8.8 built for
i386-linux-thread-multi (CentOS). I've also reproduced on Ubuntu 8.10
(64bit).
Subject: | related.t |
use Test::More tests=>7;
BEGIN { use_ok('Related') }
unlink('links.db');
$db = Related->connect("dbi:SQLite:links.db");
isa_ok($db, 'Related');
$db->storage->dbh->do('create table a ( a_id integer primary key )');
$db->storage->dbh->do('create table b ( b_id integer primary key )');
$db->storage->dbh->do('create table ab ( a_id integer, b_id integer )');
my $a = $db->resultset('A')->create({'a_id'=>1});
isa_ok($a, 'Related::A');
my $b = $db->resultset('B')->create({'b_id'=>1});
isa_ok($b, 'Related::B');
$link = $a->create_related('links', { 'b_id' => $b } );
isa_ok($link, 'Related::AB');
is($db->resultset('AB')->count, 1);
$link = $a->delete_related('links', { 'b_id' => $b } );
is($db->resultset('AB')->count, 0);
Subject: | Related.pm |
package Related::B;
use base 'DBIx::Class';
__PACKAGE__->load_components("Core");
__PACKAGE__->table("b");
__PACKAGE__->add_columns( "b_id", { data_type => "integer", is_nullable => 0, size => undef } );
__PACKAGE__->set_primary_key("b_id");
package Related::A;
use base 'DBIx::Class';
__PACKAGE__->load_components("Core");
__PACKAGE__->table("a");
__PACKAGE__->add_columns( "a_id", { data_type => "integer", is_nullable => 0, size => undef } );
__PACKAGE__->set_primary_key("a_id");
__PACKAGE__->has_many( "links", "Related::AB", { "foreign.a_id" => "self.a_id" } );
__PACKAGE__->many_to_many( "bs" => 'links', 'b_id' );
package Related::AB;
use base 'DBIx::Class';
__PACKAGE__->load_components("Core");
__PACKAGE__->table("ab");
__PACKAGE__->add_columns( "a_id", { data_type => "integer", is_nullable => 0, size => undef } );
__PACKAGE__->add_columns( "b_id", { data_type => "integer", is_nullable => 0, size => undef } );
__PACKAGE__->set_primary_key("a_id", "b_id");
__PACKAGE__->belongs_to( "a_id" => "Related::A" );
__PACKAGE__->belongs_to( "b_id" => "Related::B" );
package Related;
use base 'DBIx::Class::Schema';
__PACKAGE__->load_classes( qw/ A AB B / );
1;