Search code examples
perldbix-class

index is not visible to DBIx::Class


I have this small perl code which is going to add a record to a table but I am confused why DBIC is not able to see the primary key?

I am not able to find any answer anywhere. First the names of table and columns were camelCase, which I then changed to underscore but it just won't run :(

$ ./test.pl
DBIx::Class::ResultSource::unique_constraint_columns(): Unknown unique constraint node_id on 'node' at ./test.pl line 80

code:

sub addNode
{
    my $node = shift; my $lcNode = lc($node);
    my $id = $schema
        ->resultset('Node')
        ->find_or_create
        (
            { node_name => $lcNode },
            { key => 'node_id' }
        );
    return $id;
}

table details:

mysql> desc node;
+------------+-----------------------+------+-----+---------+----------------+
| Field      | Type                  | Null | Key | Default | Extra          |
+------------+-----------------------+------+-----+---------+----------------+
| node_id    | mediumint(5) unsigned | NO   | PRI | NULL    | auto_increment |
| node_name  | varchar(50)           | NO   |     | NULL    |                |
| node_notes | varchar(1000)         | YES  |     | NULL    |                |
+------------+-----------------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

DBIx::Class::Resultset:

$ cat Node.pm
use utf8;
package Testdb::Schema::Result::Node;

# Created by DBIx::Class::Schema::Loader
# DO NOT MODIFY THE FIRST PART OF THIS FILE

use strict;
use warnings;

use base 'DBIx::Class::Core';
__PACKAGE__->table("node");
__PACKAGE__->add_columns(
  "node_id",
  {
    data_type => "mediumint",
    extra => { unsigned => 1 },
    is_auto_increment => 1,
    is_nullable => 0,
  },
  "node_name",
  { data_type => "varchar", is_nullable => 0, size => 50 },
  "node_notes",
  { data_type => "varchar", is_nullable => 1, size => 1000 },
);
__PACKAGE__->set_primary_key("node_id");


# Created by DBIx::Class::Schema::Loader v0.07045 @ 2017-08-21 22:14:58
# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:bWXf98hpLJgNBU93aaRYkQ


# You can replace this text with custom code or comments, and it will be preserved on regeneration
1;

Solution

  • The real ans which came in DBIx list from Henry, is, whatever key you use, the col it corresponds to, should be in the query. all of above are ambiguous, they are not wrong, but they do not clarify the precise fact.