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

我有两张表employees和salary,Salary is belongs_to employees 我喜欢用employees表中的员工编号搜索薪水

当我尝试运行它时,我收到以下错误

DBIx::Class::ResultSource::_resolve_join(): No such relationship employees on Salary at /var/www/Employees/Testing/3_simpleJoin.pl line 29

这是我的课程:

薪水:

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

2 回答 2

1

尝试定义这样的belongs_to关系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;

另外,请帮自己一个忙,并始终使用strict.

于 2013-05-24T11:56:19.397 回答
1

从那个错误中,我猜你没有在你的 Result 类中为 Salary 定义关系。查看DBIC 关系手册,如果没有帮助,请使用 Result 类更新您的问题。

更新:您已经在Salary. 您的join行不正确,将其更改为join => 'emp_no'应该可以解决错误。

于 2013-05-24T03:53:51.167 回答