find_or_create is a method on DBIx::Class::ResultSet On Wed, Oct 12, 2011 at 12:06, Rajeev Prasad <[email protected]> wrote:
> > > > there is a find_and_create function which can do the job, i am not able to > find help on it. does anyone know? > > > > > ------------------------------ > *From:* Rajeev Prasad <[email protected]> > *To:* "[email protected]" <[email protected]> > *Sent:* Wednesday, October 12, 2011 10:33 AM > *Subject:* [Dbix-class] how to avoid adding duplicate row to table > containing unique field/column > > Hello, > > In my test script below, i am trying to figure out how to suppress the > error mesg when the the script try to add a previously presen tentry into > the table. > > OR better even, to make sure that the script should not even try to update > the table, if the record is already present in table. (table may grow very > big in future). > > > table:ActionKeynameTypeUniquePackedColumnCardinalityCollationNullComment > [image: > Edit] > Edit<http://www.sking.att/phpMyAdmin/tbl_indexes.php?db=mysqltestdb&table=testdbusers&index=PRIMARY&token=6f24cb5736aa4533f388248e941da70f> > [image: > Drop] > Drop<http://www.sking.att/phpMyAdmin/sql.php?db=mysqltestdb&table=testdbusers&sql_query=ALTER+TABLE+%60testdbusers%60+DROP+PRIMARY+KEY&message_to_show=The+primary+key+has+been+dropped&token=6f24cb5736aa4533f388248e941da70f> > PRIMARYBTREEYesNouserid3A > > [image: Edit] > Edit<http://www.sking.att/phpMyAdmin/tbl_indexes.php?db=mysqltestdb&table=testdbusers&index=userid&token=6f24cb5736aa4533f388248e941da70f> > [image: > Drop] > Drop<http://www.sking.att/phpMyAdmin/sql.php?db=mysqltestdb&table=testdbusers&sql_query=ALTER+TABLE+%60testdbusers%60+DROP+INDEX+%60userid%60&message_to_show=Index+userid+has+been+dropped&token=6f24cb5736aa4533f388248e941da70f> > useridBTREEYesNouserid3A > > > mysql> desc testdbusers; > +----------+-------------+------+-----+---------+-------+ > | Field | Type | Null | Key | Default | Extra | > +----------+-------------+------+-----+---------+-------+ > | userid | char(6) | NO | PRI | NULL | | > | password | varchar(10) | YES | | NULL | | > +----------+-------------+------+-----+---------+-------+ > 2 rows in set (0.00 sec) > > mysql> > > mysql> show index from testdbusers; > > +-------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+ > | Table | Non_unique | Key_name | Seq_in_index | Column_name | > Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | > > +-------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+ > | testdbusers | 0 | PRIMARY | 1 | userid | > A | 3 | NULL | NULL | | BTREE | | > | testdbusers | 0 | userid | 1 | userid | > A | 3 | NULL | NULL | | BTREE | | > > +-------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+ > 2 rows in set (0.00 sec) > > mysql> > > > > > script: > > #!/usr/bin/perl > use strict; > use warnings; > use lib '../testdb'; > use Mysqltestdb::Schema; > my $schema; > my @newusers; > $schema = > Mysqltestdb::Schema->connect("dbi:mysql:dbname=mysqltestdb:localhost:3306", > 'root', 'mypassword'); > @newusers = (['te1234', 'pass'], ['te4567', 'pass']); > $schema->populate('Testdbuser', [ > [qw/userid password/], > @newusers, > ]); > > > > > I get this info message out from script(error): > > DBIx::Class::Schema::populate(): Duplicate entry 'te1234' for key 'PRIMARY' > for populate slice: > { > password => "pass", > userid => "te1234" > } > > > > _______________________________________________ > List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class > IRC: irc.perl.org#dbix-class > SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/ > Searchable Archive: > http://www.grokbase.com/group/[email protected] > > > _______________________________________________ > List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class > IRC: irc.perl.org#dbix-class > SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/ > Searchable Archive: > http://www.grokbase.com/group/[email protected] > -- Thanks, Rob Kinyon
_______________________________________________ List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class IRC: irc.perl.org#dbix-class SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/ Searchable Archive: http://www.grokbase.com/group/[email protected]
