[CDBI] Working with tables without primary keys?

Eamon Daly edaly at nextwavemedia.com
Tue Nov 8 16:50:43 GMT 2005


I didn't realize that 'All' makes the left-hand item the PK.
My hack was to define all columns as the PK, which seems to
get me where I want to go. To answer the question "why?",
though, I would think this is a pretty common technique:

package Test::Action;
__PACKAGE__->columns(Primary => qw/person_id action/);

package Test::Person;
__PACKAGE__->columns(All => qw/person_id person_name/);

...

my @actions = Test::Action->retrieve_all;

for (sort { $a->person_id cmp $b->person_id } @actions) {
    print join("\t", $_->person_id, $_->action) . "\n";
}

print "---\n";

my @people = Test::Person->retrieve_all;

for (sort { $a->person_id cmp $b->person_id } @people) {
    print join("\t", $_->person_id, $_->person_name) . "\n";

    my @actions = Test::Action->search({ person_id => $_->person_id });
    print map { "\t" . $_->action . "\n" } @actions;
    print "\n";
}

This works as I'd expect:

1       Ate lunch
2       Went potty
2       Ate lunch
3       Went potty
3       Washed hands
3       Ate lunch
---
1       Tom
        Ate lunch

2       Dick
        Went potty
        Ate lunch

3       Harriet
        Went potty
        Washed hands
        Ate lunch

With person_id as the PK (using 'All'), this obviously
breaks:

1       Ate lunch
2       Went potty
2       Went potty
3       Went potty
3       Went potty
3       Went potty
---
1       Tom
        Ate lunch

2       Dick
        Went potty
        Went potty

3       Harriet
        Went potty
        Went potty
        Went potty

Of course, the Right Thing would be to add a true PK to the
action table.

____________________________________________________________
Eamon Daly



----- Original Message ----- 
From: "Perrin Harkins" <perrin at elem.com>
To: "Matt S Trout" <dbix-class at trout.me.uk>
Cc: "Eamon Daly" <edaly at nextwavemedia.com>; 
<classdbi at svr02.digitalcraftsmen.net>
Sent: Monday, November 07, 2005 5:51 PM
Subject: Re: [CDBI] Working with tables without primary keys?


> On Mon, 2005-11-07 at 23:44 +0000, Matt S Trout wrote:
>> I *do* know that DBIx::Class does handle this; just build a custom base 
>> class
>> that doesn't include the PK component and you should get at least some
>> functionality
>
> What possible functionality can an O/R mapper provide when it has no way
> to individually address rows?  Something through class methods?
>
> - Perrin
> 





More information about the ClassDBI mailing list