[CDBI] problem with no primary key?

Matt S Trout dbix-class at trout.me.uk
Fri Nov 25 19:25:32 GMT 2005

On Fri, Nov 25, 2005 at 08:00:23PM +0100, Tilman Baumann wrote:
> package LicServ::Option;
> use base 'LicServ::DBI';
> LicServ::Option->table('option');
> LicServ::Option->columns(Key => qw(name product_id value));
> LicServ::Option->has_a(product_id => 'LicServ::Product');

You haven't defined the Essential column group to tell Class::DBI which
columns to select when it retrieves an object.

Defining All implicitly defines Essential, if not already defined. 

Also note that you *must* have a primary key. If you think you don't have one,
the primary key is effectively all columns - i.e.

LicServ::Option->columns(All => qw(name product_id value));
LicServ::Option->columns(Primary => qw(name product_id value));

Will probably give you the behaviour you're looking for.
> I get
> -----------
> LicServ::Option can't SELECT
> FROM option
> WHERE product_id = ?
> : DBD::Pg::st execute failed: FEHLER: Fehler �Syntaxfehler� bei �FROM�
> at character 9
> [for Statement "SELECT
> FROM option
> WHERE product_id = ?
> " with ParamValues: 1='CBS-1'] at
> /usr/share/perl5/DBIx/ContextualFetch.pm line 52.
> at /usr/share/perl5/Class/DBI/Relationship/HasMany.pm line 118
> -----------
> on a foreach my $option ($product->options)
> FEHLER: Fehler �Syntaxfehler� bei �FROM� at character 9 -> in English is
> ERROR: Error Syntaxerror near FROM at character 9
> Strangely Postgres has a german locale. I thought is was running as root
> ans so should have none... Howerver. :)
> If i type SELECT * FROM option WHERE product_id = 'CBS-1' i get the
> correct anser. So i think the problem is that no SELECT _what_ is given.
> Any idea how to avoid this problem?

Fix your class definition :)

Class::DBI is doing exactly what you've told it to, albeit in this case
that isn't actually particularly useful.

     Matt S Trout       Specialists in Perl consulting, web development, and
  Technical Director    UNIX/Linux systems architecture and automation. Mail
Shadowcat Systems Ltd.  mst (at) shadowcatsystems.co.uk for more information

 + Help us build a better perl ORM: http://dbix-class.shadowcatsystems.co.uk/ +

More information about the ClassDBI mailing list