我是否需要手动为 DBIx::Class own_to 关系创建索引

发布于 2024-10-18 22:31:08 字数 3512 浏览 1 评论 0原文

我正在使用 DBIx::Class 模块对我拥有的应用程序进行 ORM 方法。

我的人际关系遇到了一些问题。

我有以下模块

package MySchema::Result::ClusterIP;
use strict;
use warnings;

use base qw/DBIx::Class::Core/;

our $VERSION = '1.0';

__PACKAGE__->load_components(qw/InflateColumn::Object::Enum Core/);
__PACKAGE__->table('cluster_ip');

__PACKAGE__->add_columns( # Columns here );

__PACKAGE__->set_primary_key('objkey');
__PACKAGE__->belongs_to( 'configuration' => 'MySchema::Result::Configuration', 'config_key');
__PACKAGE__->belongs_to( 'cluster' => 'MySchema::Result::Cluster',
                            { 'foreign.config_key' => 'self.config_key',
                              'foreign.id'         => 'self.cluster_id'
                            }
                        );

以及

package MySchema::Result::Cluster;
use strict;
use warnings;

use base qw/DBIx::Class::Core/;

our $VERSION = '1.0';

__PACKAGE__->load_components(qw/InflateColumn::Object::Enum Core/);
__PACKAGE__->table('cluster');

__PACKAGE__->add_columns(  # Columns here );
__PACKAGE__->set_primary_key('objkey');
__PACKAGE__->belongs_to( 'configuration' => 'MySchema::Result::Configuration', 'config_key');
__PACKAGE__->has_many('cluster_ip'  => 'MySchema::Result::ClusterIP',
                            { 'foreign.config_key' => 'self.config_key',
                              'foreign.cluster_id' => 'self.id'
                            });

其他几个模块,但我不认为它们是相关的。

当我尝试部署此模式时,出现以下错误:

DBIx::Class::Schema::deploy(): DBI Exception: DBD::mysql::db do failed: Can't create table 'test.cluster_ip' (errno: 150) [
for Statement "CREATE TABLE `cluster_ip` (
  `objkey` smallint(5) unsigned NOT NULL auto_increment,
  `config_key` smallint(5) unsigned NOT NULL,
  `cluster_id` char(16) NOT NULL,
  INDEX `cluster_ip_idx_config_key_cluster_id` (`config_key`, `cluster_id`),
  INDEX `cluster_ip_idx_config_key` (`config_key`),
  PRIMARY KEY (`objkey`),
  CONSTRAINT `cluster_ip_fk_config_key_cluster_id` FOREIGN KEY (`config_key`, `cluster_id`) REFERENCES `cluster` (`config_key`, `id`) ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT `cluster_ip_fk_config_key` FOREIGN KEY (`config_key`) REFERENCES `configuration` (`config_key`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB"] at test_deploy.pl line 18

 (running "CREATE TABLE `cluster_ip` (
  `objkey` smallint(5) unsigned NOT NULL auto_increment,
  `config_key` smallint(5) unsigned NOT NULL,
  `cluster_id` char(16) NOT NULL,
  INDEX `cluster_ip_idx_config_key_cluster_id` (`config_key`, `cluster_id`),
  INDEX `cluster_ip_idx_config_key` (`config_key`),
  PRIMARY KEY (`objkey`),
  CONSTRAINT `cluster_ip_fk_config_key_cluster_id` FOREIGN KEY (`config_key`, `cluster_id`) REFERENC
ES `cluster` (`config_key`, `id`) ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT `cluster_ip_fk_config_key` FOREIGN KEY (`config_key`) REFERENCES `configuration` (`conf
ig_key`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB") at test_deploy.pl line 18

据我所知,MySQL 抱怨 FOREIGN KEY 约束,特别是对 (config_key, id< /code>)在cluster表中。从我对 MySQL 文档的阅读来看,这似乎是一个合理的抱怨,特别是关于 此文档页面

这是我的问题。我是否缺少 DBIx::Class 模块中的某些内容?我意识到我可以显式创建必要的索引来匹配这个外键约束,但这似乎是重复性的工作。我应该做些什么来让这种情况隐式发生吗?

I'm using the DBIx::Class modules for an ORM approach to an application I have.

I'm having some problems with my relationships.

I have the following

package MySchema::Result::ClusterIP;
use strict;
use warnings;

use base qw/DBIx::Class::Core/;

our $VERSION = '1.0';

__PACKAGE__->load_components(qw/InflateColumn::Object::Enum Core/);
__PACKAGE__->table('cluster_ip');

__PACKAGE__->add_columns( # Columns here );

__PACKAGE__->set_primary_key('objkey');
__PACKAGE__->belongs_to( 'configuration' => 'MySchema::Result::Configuration', 'config_key');
__PACKAGE__->belongs_to( 'cluster' => 'MySchema::Result::Cluster',
                            { 'foreign.config_key' => 'self.config_key',
                              'foreign.id'         => 'self.cluster_id'
                            }
                        );

As well as

package MySchema::Result::Cluster;
use strict;
use warnings;

use base qw/DBIx::Class::Core/;

our $VERSION = '1.0';

__PACKAGE__->load_components(qw/InflateColumn::Object::Enum Core/);
__PACKAGE__->table('cluster');

__PACKAGE__->add_columns(  # Columns here );
__PACKAGE__->set_primary_key('objkey');
__PACKAGE__->belongs_to( 'configuration' => 'MySchema::Result::Configuration', 'config_key');
__PACKAGE__->has_many('cluster_ip'  => 'MySchema::Result::ClusterIP',
                            { 'foreign.config_key' => 'self.config_key',
                              'foreign.cluster_id' => 'self.id'
                            });

There are a couple of other modules, but I don't believe that they are relevant.

When I attempt to deploy this schema, I get the following error:

DBIx::Class::Schema::deploy(): DBI Exception: DBD::mysql::db do failed: Can't create table 'test.cluster_ip' (errno: 150) [
for Statement "CREATE TABLE `cluster_ip` (
  `objkey` smallint(5) unsigned NOT NULL auto_increment,
  `config_key` smallint(5) unsigned NOT NULL,
  `cluster_id` char(16) NOT NULL,
  INDEX `cluster_ip_idx_config_key_cluster_id` (`config_key`, `cluster_id`),
  INDEX `cluster_ip_idx_config_key` (`config_key`),
  PRIMARY KEY (`objkey`),
  CONSTRAINT `cluster_ip_fk_config_key_cluster_id` FOREIGN KEY (`config_key`, `cluster_id`) REFERENCES `cluster` (`config_key`, `id`) ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT `cluster_ip_fk_config_key` FOREIGN KEY (`config_key`) REFERENCES `configuration` (`config_key`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB"] at test_deploy.pl line 18

 (running "CREATE TABLE `cluster_ip` (
  `objkey` smallint(5) unsigned NOT NULL auto_increment,
  `config_key` smallint(5) unsigned NOT NULL,
  `cluster_id` char(16) NOT NULL,
  INDEX `cluster_ip_idx_config_key_cluster_id` (`config_key`, `cluster_id`),
  INDEX `cluster_ip_idx_config_key` (`config_key`),
  PRIMARY KEY (`objkey`),
  CONSTRAINT `cluster_ip_fk_config_key_cluster_id` FOREIGN KEY (`config_key`, `cluster_id`) REFERENC
ES `cluster` (`config_key`, `id`) ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT `cluster_ip_fk_config_key` FOREIGN KEY (`config_key`) REFERENCES `configuration` (`conf
ig_key`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB") at test_deploy.pl line 18

From what I can tell, MySQL is complaining about the FOREIGN KEY constraint, in particular, the REFERENCE to (config_key, id) in the cluster table. From my reading of the MySQL documentation, this seems like a reasonable complaint, especially in regards to the third bullet point on this doc page.

Here's my question. Am I missing something in the DBIx::Class module? I realize that I could explicitly create the necessary index to match up with this foreign key constraint, but that seems to be repetitive work. Is there something I should be doing to make this occur implicitly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清晨说晚安 2024-10-25 22:31:08

不确定这里发生了什么 SQL::Translator:: Producer::MySQL 应在部署的 DDL 开头插入 SETforeign_key_checks=0,这样就不会出现外键错误。我怀疑即使部署了整个 DDL 之后,某些东西还是被破坏了。您可以通过连接到数据库并运行以下语句来找出外键错误的确切性质:

SHOW INNODB STATUS;

Not sure what is happening here as SQL::Translator::Producer::MySQL should insert SET foreign_key_checks=0 at the start of the deployed DDL so no foreign key errors should occur. I suspect something is broken even after the whole DDL is deployed. You can find out the exact nature of the foreign key error by connecting to the DB and running this statement:

SHOW INNODB STATUS;
岁月如刀 2024-10-25 22:31:08

我刚刚通过将以下内容添加到导致问题的结果类中来解决同样的问题:

sub sqlt_deploy_hook {
   my ($self, $sqlt_table) = @_;
   $sqlt_table->add_index(name => 'idx_my_column', fields => ['my_column']);
}

I just fixed this same problem by adding the following into my result classes that were causing problems:

sub sqlt_deploy_hook {
   my ($self, $sqlt_table) = @_;
   $sqlt_table->add_index(name => 'idx_my_column', fields => ['my_column']);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文