[Catalyst] Trouble inserting data after migrate database from SQLite to My SQL

J. Shirley jshirley at gmail.com
Tue Nov 10 15:20:26 GMT 2009


On Tue, Nov 10, 2009 at 5:53 AM, Jordi Amor=F3s <jamoros at etsetb.upc.edu>wro=
te:

> Hi all,
>
> I'm developing a little web application (a remote lab that works with
> Catalyst+LabView). Until now I was using SQLite, but due to new
> requirements I've had to migrate the database.
>
> I've created a new model:
> perl script/ilabrs_create.pl model DB DBIC::Schema ilabrs::Schema
> create=3Dstatic components=3DTimeStamp,EncodedColumn dbi:mysql:ilabrs
>
> and then, after restarting the server all seemed to work. Actually,
> list, edit and erase from the database is working fine. But when I want
> to add data, it doesn't work.
>
> The problem appears when an id it's not provided. If I'm not wrong,
> "my $exp =3D $c->model('DB::Activitats')-> find_or_new({id=3D> $id});"
> should solve that. But: "$c->log->debug("Experiment ID:".$exp->id);",
> prints nothing.
>
>
I'm absolutely clueless. I've tried to split de edit function in two:
> -one to actually edit (my $exp =3D $c->model('DB::Activitats')->
> find({id=3D> $id});) which still works properly.
>
> -and a second one to add (my $exp =3D $c->model('DB::Activitats')->
> new({});) which doesn't because $exp->id is empty and
> $exp->update_or_insert;
> cause an exception:
>
> DBI Exception: DBD::mysql::st execute failed: Column 'id' cannot be
> null...
>
> Any suggestion, idea, clue... will be welcome.
>
> Thanks,
>
>
This is a DBIC question, as such the answer is clearly found in the DBIC
manual:
http://search.cpan.org/~ribasushi/DBIx-Class-0.08112/lib/DBIx/Class/ResultS=
et.pm#find_or_new

To summarize, ->new or ->find_or_new will not create a record in the
database (and as such, no id is available).  If you want to create a record,
triggering the auto_increment, then you must use create or a subsequent
->insert after a ->new call.

Also of note, DBIx::Class has its own mailing list that you should consider
subscribing to.

Thanks,
-Jay
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/catalyst/attachments/20091110/fb27c=
6c3/attachment.htm


More information about the Catalyst mailing list