[Catalyst] How to insert from form to MySQL with autoincrement primary key?

Paul Henrich paul at worldhive.net
Sun Dec 9 20:19:39 GMT 2007


Hi Martin,

You might check to make sure that you are loading PK::Auto in  
MyAppDB::MyTable:

# ...

use base 'DBIx::Class';
__PACKAGE__->load_components("PK::Auto", "Core");
__PACKAGE__->table("mytable");

# then make sure you declare the primary key later on...
__PACKAGE__->set_primary_key("mytable_id");


All the best,
Paul


On 9 Dec 2007, at 6:20 AM, Martin Ellison wrote:

> Sorrry, this is another newbie question.
>
> I am using HTML::Widget and DBIx::Class to do simple CRUD on some  
> MySQL tables. I have defined an auto-increment primary key on one  
> of my tables, and I want to use an HTML form to populate and create  
> a new object. I then want to retrieve the newly-generated key.
>
> Sup[pose the SQL has:
>
> CREATE TABLE mytable (
>        mytable_id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
>        title VARCHAR(64) NOT NULL,
>        date VARCHAR(10) NOT NULL,
>        PRIMARY KEY (mytable_id);
>
> Having read the examples, I thought that all I had to do is:
>
>             my $obj = $c->model(q{MyAppDB::MyTable})->new({});
>             $obj->populate_from_widget($result);
>             my $newid = $obj->mytable_id;
>
> but I am finding that the generated SQL is in fact setting the key  
> field to zero, and then the last line of the above code is getting  
> an undefined value back.
>
> But then I have not actually told my DBIx::Class package for the  
> table that the column is autoincrement -- do I need to do this and  
> if so how?
>
> -- 
> Regards,
> Martin
> (m.e at acm.org)
> IT: http://methodsupport.com Personal: http://thereisnoend.org
> _______________________________________________
> List: Catalyst at lists.scsys.co.uk
> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
> Searchable archive: http://www.mail-archive.com/ 
> catalyst at lists.rawmode.org/
> Dev site: http://dev.catalyst.perl.org/

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/catalyst/attachments/20071209/f6b238b0/attachment.htm


More information about the Catalyst mailing list