在 RDBO 中延迟且从未创建的外键关系方法

Foreign key relationship methods deferred and never created in RDBO

我正在 employees test dataset 上玩 RoseDB::Object,由于某种原因,我无法获取外键关系('department' 和 'employee')处理 DeptEmp 对象。 (Class 结构如下)。

当我尝试 $e->dept_emp->[0]->department 时,我得到:

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

我确定我的 class 结构设置有误,但是什么?

CLASS 结构(为清楚起见省略了一些 class):

我使用 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;

您的外键有错别字:

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

应该是'Department',不是'Departments'

原来是我的代码有误。 DeptEmp.pm 中的这些行:

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

    employee => {
        class       => 'My::FakeEmployees::Employees',
        key_columns => { emp_no => 'emp_no' },
    },
],

有不正确的 class 名字。它应该是 My::FakeEmployees::Employee 和 My::FakeEmployees::Department。单数,不是复数。