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

标签 mysql perl rose-db-object

我在 employees test dataset 上玩 RoseDB::Object ,并且出于某种原因,我无法让我的外键关系(“部门”和“员工”)在 DeptEmp 对象上工作。 (下面的类结构)。

当我尝试 $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

我确定我的类结构中设置了错误,但是什么?

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

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

关于mysql - 在 RDBO 中延迟且从未创建的外键关系方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31465355/

相关文章:

regex - 正则表达式从命令历史记录中获取已执行的脚本名称

string - 在 perl 中获取字符串的 hahes 键

Perl 和 Rose::DB Postgres 事务

mysql - SQL硬查询: events between other events

php - 如何在php中计算每个级别的15级深度的成员

html - MySQL SELECT * FROM ... WHERE ... ORDER BY 'which one has the latest record in other table '

javascript 获取 : can't get POST response data from perl script

perl - 我可以从 Rose::DB::Object 元数据创建表(如果不存在)吗?

perl - 如何使用 RDBO 在 Template Toolkit 中强制列出上下文?

mysql - 我可以加快这个 mysql 查询吗?