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;

I have two tables employees and salary and Salary is belongs_to employees i like to search the salary with employee number which is in employees table

when i try to run it i get the following error

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

here are my calsses :

Salary:

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;

here is my Employee class

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;
pavan
  • 334
  • 6
  • 20

2 Answers2

1

From that error, I'm guessing that you didn't define the relationship inside your Result class for Salary. Have a look at the manual for DBIC Relationships, and if that doesn't help, update your question with the Result classes.

Update: You've got the relationship defined in Salary. Your join line is incorrect, change it to join => 'emp_no' and that should fix the error.

Joel
  • 3,435
  • 2
  • 23
  • 33
1

Try to define the belongs_to relationship of Salary like this:

__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" },
);

Then, in your main script:

my $salaries = $schema->resultset('Salary')->search({   
  'employee.emp_no' => 100000,  # was "employees"
}, {
  join => 'employee',           # was "employees"
});

print $salaries->count;

Also, do yourself a favor and always use strict.

nwellnhof
  • 32,319
  • 7
  • 89
  • 113
  • Thanks i got it. but actually i created the database objects by using schema loader make_schema_at( 'DAO::Schema', { debug => 1, dump_directory => '.', }, [ "dbi:mysql:dbname=employees","root", "root", ] ); but it all my atabase objects which has belongs_to relation ship it created emp_no has the table alias name , how can i change it to deafult table_name. – pavan May 25 '13 at 01:24
  • I don't know how `DBIx::Class::Schema::Loader` generates the accessor names for relations. It probably looks at the names of foreign key constraints. Can you post your original SQL schema definitions? Also, the generated `{ emp_no => "emp_no" }` condition looks bogus. Maybe it helps to use the latest version of `DBIx::Class::Schema::Loader`. – nwellnhof May 25 '13 at 13:09
  • CREATE TABLE `salaries` ( `emp_no` int(11) NOT NULL, `salary` int(11) NOT NULL, `from_date` date NOT NULL, `to_date` date NOT NULL, PRIMARY KEY (`emp_no`,`from_date`), KEY `emp_no` (`emp_no`), CONSTRAINT `salaries_ibfk_1` FOREIGN KEY (`emp_no`) REFERENCES `employees` (`emp_no`) ON DELETE CASCADE ) ENGINE=InnoDB DEFAULT CHARSET=latin1$$ – pavan May 25 '13 at 14:27