給定一個數據庫架構這樣導航關係和進一步約束結果集與查詢
create table account (
id serial primary key,
no varchar
);
create type sourcetype as enum ('TYPE_A','TYPE_B');
create table orgunit (
id serial primary key,
name varchar,
source sourcetype
);
-- N:M-Relationship between Accounts and OrgUnits
create table orgunitaccount (
orgunitid int references orgunit(id),
accountid int references account(id)
);
使用在以下類此架構結果dbicdump
(注意,我切出的註釋):
use utf8;
package RelTest::Result::Account;
use strict;
use warnings;
use base 'DBIx::Class::Core';
__PACKAGE__->table("account");
__PACKAGE__->add_columns(
"id",
{
data_type => "integer",
is_auto_increment => 1,
is_nullable => 0,
sequence => "account_id_seq",
},
"no",
{
data_type => "text",
is_nullable => 1,
original => { data_type => "varchar" },
},
);
__PACKAGE__->set_primary_key("id");
__PACKAGE__->has_many(
"orgunitaccounts",
"RelTest::Result::Orgunitaccount",
{ "foreign.accountid" => "self.id" },
{ cascade_copy => 0, cascade_delete => 0 },
);
1;
use utf8;
package RelTest::Result::Orgunit;
__PACKAGE__->table("orgunit");
__PACKAGE__->add_columns(
"id",
{
data_type => "integer",
is_auto_increment => 1,
is_nullable => 0,
sequence => "orgunit_id_seq",
},
"name",
{
data_type => "text",
is_nullable => 1,
original => { data_type => "varchar" },
},
"source",
{
data_type => "enum",
extra => { custom_type_name => "sourcetype", list => ["TYPE_A", "TYPE_B"] },
is_nullable => 1,
},
);
__PACKAGE__->set_primary_key("id");
__PACKAGE__->has_many(
"orgunitaccounts",
"RelTest::Result::Orgunitaccount",
{ "foreign.orgunitid" => "self.id" },
{ cascade_copy => 0, cascade_delete => 0 },
);
1;
use utf8;
package RelTest::Result::Orgunitaccount;
__PACKAGE__->table("orgunitaccount");
__PACKAGE__->add_columns(
"orgunitid",
{ data_type => "integer", is_foreign_key => 1, is_nullable => 1 },
"accountid",
{ data_type => "integer", is_foreign_key => 1, is_nullable => 1 },
);
__PACKAGE__->belongs_to(
"accountid",
"RelTest::Result::Account",
{ id => "accountid" },
{
is_deferrable => 0,
join_type => "LEFT",
on_delete => "NO ACTION",
on_update => "NO ACTION",
},
);
__PACKAGE__->belongs_to(
"orgunitid",
"RelTest::Result::Orgunit",
{ id => "orgunitid" },
{
is_deferrable => 0,
join_type => "LEFT",
on_delete => "NO ACTION",
on_update => "NO ACTION",
},
);
1;
請參閱this github存儲庫中的完整示例代碼。
我正在尋找的方式來瀏覽這個關係從account
朝一套orgunit
小號進一步制約這orgunit
就是我要選擇開始,基於目標表(account
)的屬性。我目前的做法是:
my $schema = $schemaPkg->connect(...); # not important
my $account =
$schema->resultset('Account')->search(number => $acctNumber)->single();
my $typeBAccounts =
grep { $_->orgunitid->source eq 'TYPE_B'}
$account->orgunitaccounts();
我的問題是:有沒有將這一約束到導航命令的方式,像search(<queryHashRef>)
?
注意,我知道我卡恩指定調用查詢到orgunitaccounts()
,但只作用範圍是orgunitaccount
表,而我想約束的基礎上account
一個屬性集。
請粘貼DBIx :: Class結果資源而不是DDL語句,這樣可以更容易地爲您提供幫助。你也應該使用find而不是search來獲取單行,在你的情況下,通過爲number列定義一個唯一的約束並使用它。 – 2014-10-08 10:12:17
我知道,我可以使用find來獲得單個結果,但這不是我的問題的關鍵。 ;)結果來源有點渺茫......但我會盡我所能。 – sschober 2014-10-08 10:54:12