[CDBI] Working with tables without primary keys?

Eamon Daly edaly at nextwavemedia.com
Mon Nov 7 23:24:15 GMT 2005


Hi, all. I couldn't find anything in the Class::DBI CPAN
pages or on the Wiki, so perhaps one of you can provide a
pointer. What's the Right Way to work with tables lacking
primary keys?

Given a table like this:

+--------+----------+
| foo_id | foo_name |
+--------+----------+
|      0 | bar-1    |
|      8 | bar-2    |
|      3 | bar-3    |
|      8 | bar-4    |
|      7 | bar-5    |
|      4 | bar-6    |
|      7 | bar-7    |
|      4 | bar-8    |
|      0 | bar-9    |
+--------+----------+

My CDBI package looks like:

package Foo::Test;

use strict;
use base 'Foo::DBI';

__PACKAGE__->columns(All => qw/foo_id foo_name/);

1;

and the test script:

use MFD::Test;

my @objs = MFD::Test->retrieve_all;

for (sort { $a->foo_name cmp $b->foo_name } @objs) {
    print join("\t", $_->foo_id, $_->foo_name) . "\n";
}

produces output like:

0       bar-1
0       bar-1
8       bar-2
8       bar-2
3       bar-3
7       bar-5
7       bar-5
4       bar-6
4       bar-6

Note that CDBI returns several entries with the wrong
foo_name, because the foo_id is reused.

____________________________________________________________
Eamon Daly





More information about the ClassDBI mailing list