2017-08-21 6 views
0

テーブルにレコードを追加するこの小さなperlコードがありますが、なぜDBICがプライマリキーを見ることができないのか混乱していますか?インデックスはDBIxには見えません:: Class

どこでも回答が見つかりません。まず、テーブルと列の名前は、私は、アンダースコアに変更され、キャメルケースであったが、それだけでは動作しません:(

$ ./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; 
} 

テーブルの詳細:

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; 

答えて

0

ヘンリーからDBIxのリストに入ってきた本物のものは、どんなキーを使っても、それに対応する列は問合せに含まれているはずです。上記のすべてが曖昧であり、間違っているわけではありませんが、正確な事実を明確にしていません。

2

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属性は、追加の一意制約の名前として参照されます。

+0

はい私はそれを試みました。新しいエラーが発生しました: '$ ./test.pl DBIx :: Class :: ResultSource :: _ minimal_valueset_satisfying_constraint():要求された制約 'primary'、列の値が欠落しています:./testの 'node_id' .pl line 80 ' – rajeev

+0

' sub addNode { \t my $ node = shift;私の$ lcNode = lc($ノード); \t私の$ ID = $スキーマ \t \t - >結果セット( 'ノード') \t \t - > find_or_create \t \t( \t \t \t {NODE_NAME => $ lcNode}、 \t \t \t {キー=> '第一 '} \t \t); \t return $ id; } ' – rajeev

+0

@rajeevはい、あなたのコードは意味をなさないからです。主キー( 'node_id')で行を探したい場合は、探しているid値を指定する必要があります。 – melpomene

1

あなたは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

+0

ええ、私は2つの駅でこれを置き換えることを考えています。最初に検索 - >数を数えれば1を加えます。 – rajeev

関連する問題