[CDBI] Working with tables without primary keys?

Perrin Harkins perrin at elem.com
Tue Nov 8 17:08:08 GMT 2005


On Tue, 2005-11-08 at 10:50 -0600, Eamon Daly wrote:
> 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.

I would say that's not a hack but rather the correct solution.  Multi-
column primary keys are normal in a table like this.

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

If you use the RetrievAll plugin, you can specify a sort field for
retrieve_all instead of doing it in perl.

> 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 });

If you use a has_many relationship in Person, you can get it to do this
for you.  It will sort them for you too.

- Perrin





More information about the ClassDBI mailing list