索引对 DBIx::Class 不可见

index is not visible to DBIx::Class

我有这个小的 perl 代码,它将向 table 添加一条记录,但我很困惑为什么 DBIC 无法看到主键?

我无法在任何地方找到任何答案。首先 table 和列的名称是驼峰式,然后我将其更改为下划线,但它不会 运行 :(

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

代码:

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 详情:

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::结果集:

$ 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;

https://metacpan.org/pod/DBIx::Class::ResultSet#find:

To aid with preparing the correct query for the storage you may supply the key attribute, which is the name of a unique constraint (the unique constraint corresponding to the primary columns is always named primary).

(强调我的。)

也就是说,要使用主键,需要指定{ key => 'primary' }。查找任何其他 key 属性作为附加唯一约束的名称。

您没有明确说明 addNode 应该如何工作。但是如果你想通过 node_name 查找现有节点,你应该简单地删除 key 属性:

my $id = $schema->resultset('Node')->find_or_create(
    { node_name => $lcNode }
);

但请阅读 DBIC documentation 中的警告:

If no such constraint is found, find currently defaults to a simple search->(\%column_values) which may or may not do what you expect. Note that this fallback behavior may be deprecated in further versions. If you need to search with arbitrary conditions - use "search". If the query resulting from this fallback produces more than one row, a warning to the effect is issued, though only the first row is constructed and returned as $result_object.

您可能应该考虑向 node_name 添加唯一约束。

来自 Henry 的 DBIx 列表中的真正答案是,无论您使用什么键,它对应的 col 都应该在查询中。以上都是模棱两可的,都没有错,只是没有弄清事实。