theugywithahat has asked for the wisdom of the Perl Monks concerning the following question:

Hello friendly perl monks!

I am trying to find_or_create the following:

my $foo = MyDB::Foo->find_or_create({ name => 'name', uri => 'hello', });

with my MyDB::Foo looking like this:

package MyDB::Foo; use base MyDB; __PACKAGE__->table('foo'); __PACKAGE__->columns( ALL => ('foo_id','name','uri') ); 1;

and I get the following error:

MyDB::Foo can't SELECT FROM foo WHERE name = ? AND uri = ? : DBD::mysql::st execute failed: You have an error in your SQL syntax; + check the manual that corresponds to your MySQL server version for t +he right syntax to use near 'FROM foo WHERE name = 'name' AND uri = 'hello'' at line 2 [for Statement "SELE +CT FROM foo WHERE name = ? AND uri = ? " with ParamValues: 0='name', 1='hello'] at C:/Perl/site/lib/DBIx/Cont +extualFetch.pm line 52. at C:/Perl/site/lib/Class/DBI/Search/Basic.pm line 169

Now if I use insert instead of find_or_create, it inserts fine and all the data is in the DB like I want it.

Also, inserting it, then searching for it makes the insert work fine, search not ( which kind of makes my title a bit misleading I guess, sry )

just in case this is relevant, here is the SQL file I used to create the table:

CREATE TABLE foo ( foo_id INTEGER PRIMARY KEY AUTO_INCREMENT, name TEXT, uri TEXT );

where is my mistake?

any help appreciated...

Replies are listed 'Best First'.
Re: Class::DBI find_or_create
by theugywithahat (Initiate) on Dec 29, 2011 at 07:36 UTC

    I did find out what went wrong, but I don't know whether or how to delete the question so I shall just answer it ;)

    contrary to some documentation I read, one should explicitely declare the primary key, I thought it would automatically be the first ( that is what I read in the doc ) in the 'ALL' list. That is not the case. So MyDB::Foo now reads

    package MyDB::Foo; use base MyDB; __PACKAGE__->table('foo'); __PACKAGE__->columns( PRIMARY =>qw/foo_id/ ); __PACKAGE__->columns( ESSENTIAL =>qw/name uri/ ); ); 1;
    and all is well... ;)
      Answering your own question is a good way. If anyone gets in a similar situation, he/she can search for the question and find the answer helpful.