Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > DBIx::DBSchema::Table support for composite primary keys

Thread Tools

DBIx::DBSchema::Table support for composite primary keys

I & L Fogg
Posts: n/a

I'm experimenting with the DBIx:BSchema module to copy a proprietary
DB into mysql (via an ODBC connection to the source DB).

I pull the DB schema with a variety of DBI calls, notably column_info
and primary_key. Building the DBIx:BSchema::Table is fairly
straightforward when the primary key for the table is a single column.
However, when I have a composite primary key, it fails whenever
$table->primary_key is called.

For example, in the following fragment, I try to "fool" the Table module
into accepting multiple columns as the primary key by assigning a
comma-separated list, but the sql_create_table call barfs as soon as it
accesses primary_key.

my @pri_key = $dbh->primary_key($catalog, $schema, $table);
$table->primary_key(join(',', @pri_key));

my $sql = $table->sql_create_table($dbh2);

Checking the definition of primary_key, it is specifically excluding my
attempt to "fake it" thought its use of the regexp /^(\w*)$/ (line 279
in module DBIx:BSchema::Table).

Some brief experimentation to allow primary_key to handle a comma
separated list...

sub primary_key {
if ( defined($value) ) {
$self->{primary_key} = $value;
} else {
=> $self->{primary_key} =~ /^(\w*)(,\w*)*$/
or die "Illegal primary key: ", $self->{primary_key};
=> $2 ? $1.$2 : $1;

And the code fragment above generates the correct SQL for simple or
composite primary keys.

This seems way too simple. What else would break?

Cheers, Iain

Reply With Quote
Ben Morrow
Posts: n/a

Quoth I & L Fogg <(E-Mail Removed)>:
> => $self->{primary_key} =~ /^(\w*)(,\w*)*$/
> #aah!
> or die "Illegal primary key: ", $self->{primary_key};
> => $2 ? $1.$2 : $1;

This is wrong: $2 will only contain the last of the comma-separated
portions. Try


or simply


and just using $1.


I must not fear. Fear is the mind-killer. I will face my fear and
I will let it pass through me. When the fear is gone there will be
nothing. Only I will remain. Removed) Frank Herbert, 'Dune'
Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Pythonic ORM with support for composite primary/foreign keys? Wolfgang Keller Python 5 11-09-2007 09:48 AM
EJB3 Composite Primary Key based on EJB references' key Anon Java 0 12-08-2006 04:59 PM
[ANN] Composite Primary Keys support for Associations Dr Nic Ruby 2 08-03-2006 12:11 PM
[ANN] Composite Primary Keys for ActiveRecord 0.3.2 Dr Nic Ruby 2 07-24-2006 09:14 AM
How to link foreign keys & primary keys using python? Harry George Python 9 06-13-2006 06:29 AM