use lib '/var/www/Employees';
use DBConnect::DBConnect qw(getSchemaConnection);
BEGIN { $ENV{DBIC_TRACE} = 1 }
$schema = getSchemaConnection();
$salaries = $schema->resultset('Salary')->search(
{
'employees.emp_no'=>100000
},
{
join =>'employees'
}
);
print $emplyees->count;
我有两个员工和薪水,薪水是belongs_to员工我喜欢用雇员表中的员工编号搜索薪水
当我尝试运行它时,我收到以下错误
DBIx::Class::ResultSource::_resolve_join(): No such relationship employees on Salary at /var/www/Employees/Testing/3_simpleJoin.pl line 29
这是我的calsses:
薪酬:
package DAO::Schema::Result::Salary;
# 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__->add_columns(
"emp_no",
{ data_type => "integer", is_foreign_key => 1, is_nullable => 0 },
"salary",
{ data_type => "integer", is_nullable => 0 },
"from_date",
{ data_type => "date", datetime_undef_if_invalid => 1, is_nullable => 0 },
"to_date",
{ data_type => "date", datetime_undef_if_invalid => 1, is_nullable => 0 },
);
__PACKAGE__->set_primary_key("emp_no", "from_date");
__PACKAGE__->belongs_to(
"emp_no",
"DAO::Schema::Result::Employee",
{ emp_no => "emp_no" },
{ is_deferrable => 1, on_delete => "CASCADE", on_update => "CASCADE" },
);
1;
这是我的员工类
package DAO::Schema::Result::Employee;
# 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__->add_columns(
"emp_no",
{ data_type => "integer", is_nullable => 0 },
"birth_date",
{ data_type => "date", datetime_undef_if_invalid => 1, is_nullable => 0 },
"first_name",
{ data_type => "varchar", is_nullable => 0, size => 14 },
"last_name",
{ data_type => "varchar", is_nullable => 0, size => 16 },
"gender",
{ data_type => "enum", extra => { list => ["M", "F"] }, is_nullable => 0 },
"hire_date",
{ data_type => "date", datetime_undef_if_invalid => 1, is_nullable => 0 },
);
__PACKAGE__->set_primary_key("emp_no");
__PACKAGE__->has_many(
"salaries",
"DAO::Schema::Result::Salary",
{ "foreign.emp_no" => "self.emp_no" },
{ cascade_copy => 0, cascade_delete => 0 },
);
__PACKAGE__->has_many(
"titles",
"DAO::Schema::Result::Title",
{ "foreign.emp_no" => "self.emp_no" },
{ cascade_copy => 0, cascade_delete => 0 },
);
1;
答案 0 :(得分:1)
从那个错误中,我猜你没有在你的Result类中为Salary定义关系。看看the manual for DBIC Relationships,如果这没有帮助,请使用结果类更新您的问题。
更新:你已经在Salary
中定义了关系。您的join
行不正确,请将其更改为join => 'emp_no'
,这样可以解决错误。
答案 1 :(得分:1)
尝试定义belongs_to
的{{1}}关系,如下所示:
Salary
然后,在您的主脚本中:
__PACKAGE__->belongs_to(
"employee", # was "emp_no"
"DAO::Schema::Result::Employee",
"emp_no", # was a wrong condition
{ is_deferrable => 1, on_delete => "CASCADE", on_update => "CASCADE" },
);
另外,请帮自己一个忙,并始终使用my $salaries = $schema->resultset('Salary')->search({
'employee.emp_no' => 100000, # was "employees"
}, {
join => 'employee', # was "employees"
});
print $salaries->count;
。