Subject: | set_up_table() croaks on views with message "$table has no primary key" (with patch) |
(This bug is related to bug 17084 in Class-DBI-Loader -
http://rt.cpan.org/Public/Bug/Display.html?id=17084 )
Class::DBI::Loader calls set_up_table, which in turn croak()s if no
primary key can be found for a certain table.
As far as the mentioned method is concerned, views appear as normal
tables, but a DESCRIBE query on them shows no primary key.
My patch makes set_up_table() emit a warning instead of _croak()ing if
no primary keys are found but the table is in fact a VIEW.
The patch makes use of INFORMATION_SCHEMA.VIEWS table, therefore some
tests are needed to detect if the version of MySQL server we are talking
to supports that feature.
AFAICT this patch is NOT required if one doesn't make use of a Loader,
i.e. one is writing his/her table classes by hand.
Subject: | mysql.pm.diff |
--- /var/sambashare/mysql.pm.orig 2006-05-17 14:05:16.000000000 +0200
+++ /var/sambashare/mysql.pm.modified 2006-05-17 14:03:13.000000000 +0200
@@ -56,6 +56,7 @@
=cut
__PACKAGE__->set_sql(desc_table => 'DESCRIBE __TABLE__');
+__PACKAGE__->set_sql(is_view => 'SELECT COUNT(*) FROM INFORMATION_SCHEMA.VIEWS WHERE TABLE_SCHEMA=(SELECT DATABASE()) AND TABLE_NAME=?');
sub set_up_table {
my $class = shift;
@@ -67,7 +68,12 @@
push @cols, $col;
push @pri, $col if $hash->{key} eq "PRI";
}
- $class->_croak("$table has no primary key") unless @pri;
+ $sth->finish;
+ ($sth = $class->sql_is_view)->execute($table);
+ my $is_view = ($sth->fetchrow_array)[0];
+ $sth->finish;
+ $class->_croak("$table has no primary key") unless (@pri || $is_view);
+ $class->_carp("$table is a VIEW: cannot determine primary key") if($is_view);
$class->columns(Primary => @pri);
$class->columns(All => @cols);
}