Hello, I've written a short script using DBIx::Class::Schema. It works about like I would expect, until I added the following line in GenesDB::SegmentTypes:
'use overload '""' => 'type', fallback => 1;' (I found this in DBIx::Class::Manual::Cookbook.) When I run the script with that line uncommented, I get the following error: DBD::mysql::st execute failed: Duplicate entry '1' for key 1 at /usr/local/share/perl/5.8.7/DBIx/Class/Storage/DBI.pm line 516. DBIx::Class::AccessorGroup::__ANON__(): Error executing 'INSERT INTO segment_types (id) VALUES (?)': Duplicate entry '1' for key 1 at ./test.pl line 14 I'm not sure why it would be trying to insert a new record into segment_types. Is there something I am doing wrong? Below is the script that fails, along with the relevant classes. Thanks, Stephen #!/usr/bin/perl use warnings; use strict; use lib './'; use GenesDB; my $schema = GenesDB->connect('dbi:mysql:database', 'username', 'password', {}); my $locus = $schema->resultset('GenesDB::Loci')->find(1); print 'Ref: ', $locus->ref, "\n"; print 'Segment Type: ', $locus->segment_type_id, "\n"; package GenesDB::SegmentTypes; use strict; use warnings; use base 'DBIx::Class'; __PACKAGE__->load_components(qw/PK::Auto Core/); __PACKAGE__->table('segment_types'); __PACKAGE__->add_columns(qw/id type/); __PACKAGE__->set_primary_key('id'); use overload '""' => 'type', fallback => 1; __PACKAGE__->has_many(loci => 'Loci', 'segment_type_id'); 1; package GenesDB::Loci; use strict; use warnings; use base 'DBIx::Class'; __PACKAGE__->load_components(qw/PK::Auto Core/); __PACKAGE__->table('loci'); __PACKAGE__->add_columns(qw/id ref segment_type_id /); __PACKAGE__->set_primary_key('id'); __PACKAGE__->belongs_to(segment_type_id => 'GenesDB::SegmentTypes'); 1; package GenesDB; use base qw/DBIx::Class::Schema/; __PACKAGE__->load_classes(); 1; And the sql used to create the tables: use genes_test; SET FOREIGN_KEY_CHECKS=0; #---------------------------- # Table structure for loci #---------------------------- CREATE TABLE `loci` ( `id` int(11) NOT NULL auto_increment COMMENT 'Old SNPid', `ref` varchar(25) NOT NULL COMMENT 'reference point used to specify location', `segment_type_id` int(11) NOT NULL, PRIMARY KEY (`id`), KEY `segment_type_id` (`segment_type_id`), CONSTRAINT `loci_ibfk_3` FOREIGN KEY (`segment_type_id`) REFERENCES `segment_types` (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1; set names latin1; #---------------------------- # Records for table loci #---------------------------- insert into loci values (1, 'ORF-2576', 1); #---------------------------- # Table structure for segment_types #---------------------------- CREATE TABLE `segment_types` ( `id` int(11) NOT NULL auto_increment, `type` varchar(25) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1; set names latin1; #---------------------------- # Records for table segment_types #---------------------------- insert into segment_types values (1, ''), (2, 'Exon'), (3, 'Intron'), (4, 'Intergenic'), (5, '3\' Intergenic'); _______________________________________________ List: http://lists.rawmode.org/cgi-bin/mailman/listinfo/dbix-class Wiki: http://dbix-class.shadowcatsystems.co.uk/ IRC: irc.perl.org#dbix-class SVN: http://dev.catalyst.perl.org/repos/bast/trunk/DBIx-Class/