gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 23:56:52 24 4
gpt4 key购买 nike

我在 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com