[Dbix-class] how to avoid adding duplicate row to table containing unique field/column

Rajeev Prasad rp.neuli at yahoo.com
Wed Oct 12 15:33:27 GMT 2011


Hello,

In my test script below, i am trying to figure out how to suppress the erro=
r mesg when the the script try to add a previously presen tentry into the t=
able.

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:
Action
Keyname
Type
Unique
Packed
Column
Cardinality
Collation
Null
Comment
 Edit  Drop =

PRIMARYBTREE Yes No userid 3 A =

 =

 =

 Edit  Drop =

useridBTREE Yes No userid 3 A =

 =

 =


mysql> desc testdbusers;
+----------+-------------+------+-----+---------+-------+
| Field=A0=A0=A0 | Type=A0=A0=A0=A0=A0=A0=A0 | Null | Key | Default | Extra=
 |
+----------+-------------+------+-----+---------+-------+
| userid=A0=A0 | char(6)=A0=A0=A0=A0 | NO=A0=A0 | PRI | NULL=A0=A0=A0 |=A0=
=A0=A0=A0=A0=A0 |
| password | varchar(10) | YES=A0 |=A0=A0=A0=A0 | NULL=A0=A0=A0 |=A0=A0=A0=
=A0=A0=A0 |
+----------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql> =



mysql> show index from testdbusers;
+-------------+------------+----------+--------------+-------------+-------=
----+-------------+----------+--------+------+------------+---------+
| Table=A0=A0=A0=A0=A0=A0 | Non_unique | Key_name | Seq_in_index | Column_n=
ame | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Com=
ment |
+-------------+------------+----------+--------------+-------------+-------=
----+-------------+----------+--------+------+------------+---------+
| testdbusers |=A0=A0=A0=A0=A0=A0=A0=A0=A0 0 | PRIMARY=A0 |=A0=A0=A0=A0=A0=
=A0=A0=A0=A0=A0=A0 1 | userid=A0=A0=A0=A0=A0 | A=A0=A0=A0=A0=A0=A0=A0=A0 |=
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 3 |=A0=A0=A0=A0 NULL | NULL=A0=A0 |=A0=A0=A0=
=A0=A0 | BTREE=A0=A0=A0=A0=A0 |=A0=A0=A0=A0=A0=A0=A0=A0 |
| testdbusers |=A0=A0=A0=A0=A0=A0=A0=A0=A0 0 | userid=A0=A0 |=A0=A0=A0=A0=
=A0=A0=A0=A0=A0=A0=A0 1 | userid=A0=A0=A0=A0=A0 | A=A0=A0=A0=A0=A0=A0=A0=A0=
 |=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 3 |=A0=A0=A0=A0 NULL | NULL=A0=A0 |=A0=A0=
=A0=A0=A0 | BTREE=A0=A0=A0=A0=A0 |=A0=A0=A0=A0=A0=A0=A0=A0 |
+-------------+------------+----------+--------------+-------------+-------=
----+-------------+----------+--------+------+------------+---------+
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 =3D Mysqltestdb::Schema->connect("dbi:mysql:dbname=3Dmysqltestdb:lo=
calhost:3306", 'root', 'mypassword');
@newusers =3D (['te1234', 'pass'], ['te4567', 'pass']);

$schema->populate('Testdbuser', [
=A0=A0 [qw/userid password/],
=A0=A0 @newusers,
]);




I get this info message out from script(error):


DBIx::Class::Schema::populate(): Duplicate entry 'te1234' for key 'PRIMARY'=
 for populate slice:
{
=A0 password =3D> "pass",
=A0 userid =3D> "te1234"
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/dbix-class/attachments/20111012/255=
c6d58/attachment.htm


More information about the DBIx-Class mailing list