Note that there are some explanatory texts on larger screens.

plurals
  1. PODo I need to manually create indexes for a DBIx::Class belongs_to relationship
    primarykey
    data
    text
    <p>I'm using the <code>DBIx::Class</code> modules for an ORM approach to an application I have.</p> <p>I'm having some problems with my relationships.</p> <p>I have the following</p> <pre><code>package MySchema::Result::ClusterIP; use strict; use warnings; use base qw/DBIx::Class::Core/; our $VERSION = '1.0'; __PACKAGE__-&gt;load_components(qw/InflateColumn::Object::Enum Core/); __PACKAGE__-&gt;table('cluster_ip'); __PACKAGE__-&gt;add_columns( # Columns here ); __PACKAGE__-&gt;set_primary_key('objkey'); __PACKAGE__-&gt;belongs_to( 'configuration' =&gt; 'MySchema::Result::Configuration', 'config_key'); __PACKAGE__-&gt;belongs_to( 'cluster' =&gt; 'MySchema::Result::Cluster', { 'foreign.config_key' =&gt; 'self.config_key', 'foreign.id' =&gt; 'self.cluster_id' } ); </code></pre> <p>As well as</p> <pre><code>package MySchema::Result::Cluster; use strict; use warnings; use base qw/DBIx::Class::Core/; our $VERSION = '1.0'; __PACKAGE__-&gt;load_components(qw/InflateColumn::Object::Enum Core/); __PACKAGE__-&gt;table('cluster'); __PACKAGE__-&gt;add_columns( # Columns here ); __PACKAGE__-&gt;set_primary_key('objkey'); __PACKAGE__-&gt;belongs_to( 'configuration' =&gt; 'MySchema::Result::Configuration', 'config_key'); __PACKAGE__-&gt;has_many('cluster_ip' =&gt; 'MySchema::Result::ClusterIP', { 'foreign.config_key' =&gt; 'self.config_key', 'foreign.cluster_id' =&gt; 'self.id' }); </code></pre> <p>There are a couple of other modules, but I don't believe that they are relevant.</p> <p>When I attempt to deploy this schema, I get the following error:</p> <pre><code>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 </code></pre> <p>From what I can tell, MySQL is complaining about the FOREIGN KEY constraint, in particular, the REFERENCE to (<code>config_key</code>, <code>id</code>) in the <code>cluster</code> table. From my reading of the MySQL documentation, this seems like a reasonable complaint, especially in regards to the third bullet point on <a href="http://dev.mysql.com/doc/refman/5.5/en/innodb-foreign-key-constraints.html" rel="nofollow">this doc page</a>.</p> <p>Here's my question. Am I missing something in the <code>DBIx::Class</code> 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?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload