我正在尝试确定我是否正在使用 SQL 和 DBIx::Class 以正确的方式解决以下问题,因为我对两者都很陌生。
我有一个包含 3 个表、设备、对象和网络的架构
一个设备可以有 0..1 到多个对象 一个对象可以有 1 个网络,但一个网络可以属于 0...许多设备。
我试图在图中所示的模式中捕捉这种关系。
这是相关的模式。
设备
__PACKAGE__->table("Device");
__PACKAGE__->add_columns(
"devicename",
{ data_type => "varchar", is_nullable => 0, size => 50 },
"devicetype",
{ data_type => "varchar", is_nullable => 0, size => 20 },
"deviceid",
{ data_type => "integer", is_auto_increment => 1, is_nullable => 0 },
);
__PACKAGE__->set_primary_key("deviceid");
__PACKAGE__->has_many(
"objects",
"TestApp::Schema::Result::Object",
{ "foreign.device_deviceid" => "self.deviceid" },
{ cascade_copy => 0, cascade_delete => 0 },
目的
__PACKAGE__->table("Object");
__PACKAGE__->add_columns(
"objectid",
{ data_type => "integer", is_auto_increment => 1, is_nullable => 0 },
"objectname",
{ data_type => "varchar", is_nullable => 0, size => 100 },
"objecttype",
{ data_type => "varchar", is_nullable => 0, size => 20 },
"device_deviceid",
{ data_type => "integer", is_foreign_key => 1, is_nullable => 0 },
"network_networkid",
{ data_type => "integer", is_foreign_key => 1, is_nullable => 1 },
);
__PACKAGE__->set_primary_key("objectid");
__PACKAGE__->belongs_to(
"device_deviceid",
"TestApp::Schema::Result::Device",
{ deviceid => "device_deviceid" },
{ is_deferrable => 1, on_delete => "NO ACTION", on_update => "NO ACTION" },
);
__PACKAGE__->belongs_to(
"network_networkid",
"TestApp::Schema::Result::Network",
{ networkid => "network_networkid" },
{
is_deferrable => 1,
join_type => "LEFT",
on_delete => "NO ACTION",
on_update => "NO ACTION",
},
);
网络
__PACKAGE__->table("network");
__PACKAGE__->add_columns(
"networkid",
{ data_type => "integer", is_auto_increment => 1, is_nullable => 0 },
"network",
{ data_type => "varchar", is_nullable => 1, size => 15 },
"netmask",
{ data_type => "varchar", is_nullable => 1, size => 15 },
"cidr",
{ data_type => "integer", is_nullable => 1 },
);
__PACKAGE__->set_primary_key("networkid");
__PACKAGE__->has_many(
"objects",
"TestApp::Schema::Result::Object",
{ "foreign.network_networkid" => "self.networkid" },
{ cascade_copy => 0, cascade_delete => 0 },
);
对于我的测试数据,我正在插入如下数据。有没有更有效的方法?
#get result sets
my $network_rs = $schema->resultset('Network');
my $device_rs = $schema->resultset('Device');
#create a new device
my $new_device = $device_rs->create( { devicename => 'test_device', devicetype => 'test_dt'});
#create a new network if it doesn't exist.
my $new_network = $network_rs->find_or_create({ network => '1.1.1.1', netmask => '255.255.255.0', cidr => '24' });
#Add two objects and set the foreign key for the network table for the newly created network above.
$new_device->objects->create( { objectname => 'networkobj1',network_networkid => $new_network->networkid });
$new_device->objects->create( { objectname => 'networkobj2',network_networkid => $new_network->networkid });
要查看分配给每个网络的网络,我只需执行以下操作;
my @deviceObjects = $new_device->objects();
foreach my $object (@deviceObjects)
{
print $object->objectname . " contains the following network:\n";
print $object->network_networkid->network . "\n";
print $object->network_networkid->netmask . "\n";
print $object->network_networkid->cidr . "\n";
}