Search code examples
mysqlperlrose-db-object

Foreign key relationship methods deferred and never created in RDBO


I'm playing with RoseDB::Object on the employees test dataset, and for some reason, I can't get my foreign key relationships ('department' and 'employee') to work on the DeptEmp object. (Class structure below).

When I try $e->dept_emp->[0]->department, I get:

Can't locate object method "department" via package "My::FakeEmployees::DeptEmp"

Methods for the following relationships and foreign keys were deferred and
then never actually created in the class My::FakeEmployees::DeptEmp.

TYPE            NAME
----            ----
Foreign Key     department
Foreign Key     employee

I'm sure I have something set up wrong in my class structure, but what?

CLASS STRUCTURE (some classes omitted for clarity):

I created the various objects using the instructions in the RDBO tutorial:

package My::FakeEmployees::Employee;

use strict;

use base qw(My::FakeEmployees::DB::Object);

__PACKAGE__->meta->setup(
    table => 'employees',

    columns => [
        emp_no     => { type => 'serial',  not_null => 1 },
        birth_date => { type => 'date',    not_null => 1 },
        first_name => { type => 'varchar', length   => 14, not_null => 1 },
        last_name  => { type => 'varchar', length   => 16, not_null => 1 },
        gender     => { type => 'enum',    check_in => [ 'M', 'F' ], not_null => 1 },
        hire_date  => { type => 'date',    not_null => 1 },
    ],

    primary_key_columns => ['emp_no'],
    'relationships'     => [
        'departments' => {
            'type'      => 'many to many',
            'map_class' => 'My::FakeEmployees::DeptEmp',
        },
        'dept_emp' => {
            'type' => 'one to many',
            'class' => 'My::FakeEmployees::DeptEmp',
            'column_map' => { 'emp_no' => 'emp_no' },
        },
        'dept_manager' => {
            'type' => 'one to many',
            'class' => 'My::FakeEmployees::DeptManager',
            'column_map' => { 'emp_no' => 'emp_no' },
        },
        'salaries' => {
            'type' => 'one to many',
            'class' => 'My::FakeEmployees::Salary',
            'column_map' => { 'emp_no' => 'emp_no' },
        },
        'titles' => {
            'type' => 'one to many',
            'class' => 'My::FakeEmployees::Title',
            'column_map' => { 'emp_no' => 'emp_no' },
        },
    ],
);

__PACKAGE__->meta->make_manager_class('employees');

1;

package My::FakeEmployees::DeptEmp;

use strict;

use base qw(My::FakeEmployees::DB::Object);

__PACKAGE__->meta->setup(
    table => 'dept_emp',

    columns => [
        dept_no   => { type => 'character', not_null => 1 },
        emp_no    => { type => 'integer',   not_null => 1 },
        from_date => { type => 'date' },
        to_date   => { type => 'date' },
    ],

    primary_key_columns => [ 'emp_no', 'dept_no' ],

    foreign_keys => [
        department => {
            class       => 'My::FakeEmployees::Departments',
            key_columns => { dept_no => 'dept_no' },
        },

        employee => {
            class       => 'My::FakeEmployees::Employees',
            key_columns => { emp_no => 'emp_no' },
        },
    ],
);
__PACKAGE__->meta->make_manager_class('dept_emp');


1;

package My::FakeEmployees::Department;

use strict;

use base qw(My::FakeEmployees::DB::Object);

__PACKAGE__->meta->setup(
    table => 'departments',

    columns => [
        dept_no   => { type => 'character', length => 4,  not_null => 1 },
        dept_name => { type => 'varchar',   length => 40, not_null => 1 },
    ],

    primary_key_columns => ['dept_no'],

    unique_key => ['dept_name'],

    'relationships'     => [
        'employees' => {
            'type'      => 'many to many',
            'map_class' => 'My::FakeEmployees::DeptEmp',
        },
    ],
);
__PACKAGE__->meta->make_manager_class('departments');

1;

Solution

  • Your foreign key has a typo:

    foreign_keys => [
        department => {
            class       => 'My::FakeEmployees::Departments',
    

    That should be 'Department', not 'Departments'