Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9132169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:15:58+00:00 2026-06-17T08:15:58+00:00

Problem I’m trying to build a minimal example where I create a many-to-many relationship

  • 0

Problem

I’m trying to build a minimal example where I create a many-to-many
relationship in DBIx::Class and insert some rows into the database.

In my database I have two tables A and C which are linked by the table AC.
I try to insert the values using the following script:

#! /usr/bin/perl
use strict;
use warnings;
use DB::Main;

my $s = DB::Main->connect("dbi:SQLite:example.db");
my $a1 = $s->resultset('A')->create({a_value => 'a1'});
my $a2 = $s->resultset('A')->create({a_value => 'a2'});
my $a3 = $s->resultset('A')->create({a_value => 'a3'});

$s->resultset('C')->create({c_value => 'c1'});
$s->resultset('C')->create({c_value => 'c2'});

my $c1 = $s->resultset('C')->find(1);
$c1->add_to_as($a1);

The values a1 to a3 and c1,c2 get created just fine. When I try to
add the relationship between a1 and c1 into the table AC. I get the following error message:

Can't use an undefined value as a HASH reference at /Library/Perl/5.12/DBIx/Class/Relationship/ManyToMany.pm line 88.

I think I am following the Documentation of add_to_$rel to the letter. What am I doing wrong here?

Schema Definition

I’m using the following database schema which I deploy with DBIx::Class::DeploymentHandler into an SQLite database.

File db/Main.pm

package DB::Main;
use base qw/DBIx::Class::Schema/;

our $VERSION = 1;
__PACKAGE__->load_namespaces;
1;

File db/Main/Result/A.pm

package DB::Main::Result::A;
use base qw/DBIx::Class::Core/;
__PACKAGE__->table('a');
__PACKAGE__->add_columns(
  a_id => {
    data_type => 'Integer',
    size      => 8
  },
  a_value => {
    data_type => 'Varchar',
    size      => 50
  },
);
__PACKAGE__->set_primary_key('a_id');
__PACKAGE__->has_many('acs' => 'DB::Main::Result::AC', 'f_a_id');
__PACKAGE__->many_to_many('cs' => 'DB::Main::Result::C', 'acs');
1;

File db/Main/Result/C.pm

package DB::Main::Result::C;
use base qw/DBIx::Class::Core/;
__PACKAGE__->table('c');
__PACKAGE__->add_columns(
  c_id => {
    data_type => 'Integer',
    size      => 8
  },
  c_value => {
    data_type => 'Varchar',
    size      => 50
  },
);
__PACKAGE__->set_primary_key('c_id');
__PACKAGE__->has_many('acs' => 'DB::Main::Result::AC', 'f_c_id');
__PACKAGE__->many_to_many('as' => 'DB::Main::Result::A', 'acs');
1;

File db/Main/Result/AC.pm

package DB::Main::Result::AC;
use base qw/DBIx::Class::Core/;
__PACKAGE__->table('ac');
__PACKAGE__->add_columns(
  f_a_id => {
    data_type => 'Integer',
    size      => 8
  },
  f_c_id => {
    data_type => 'Integer',
    size      => 8
  },
);
__PACKAGE__->set_primary_key('f_a_id', 'f_c_id');
__PACKAGE__->belongs_to('a' => 'DB::Main::Result::A', 'f_a_id');
__PACKAGE__->belongs_to('c' => 'DB::Main::Result::C', 'f_c_id');
1;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T08:15:59+00:00Added an answer on June 17, 2026 at 8:15 am

    The arguments in your calls to many_to_many are wrong. Consult the documentation of many_to_many and try the following:

    __PACKAGE__->many_to_many('cs' => 'acs', 'c');
    __PACKAGE__->many_to_many('as' => 'acs', 'a');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Problem: I am trying to build a recursive tree using a function and data
Problem statement I'm trying to implement my own virtual keyboard following example softkeyboard, found
PRoblem: i'm trying to create (just for fun) a simple poker card (with a
Problem: Trying to create a Mix that is applied to the AVPlayerItem, but it
Problem in Writing the CASE statement. Please HELP insert into CDR(rts,smscid,count,statusflag,operator,identifier) select trunc(rts),smscid,statusflag,operator, (case
Problem I am trying to build a product list with (initially) the following features:
Problem: I've written State Machine for my android application. It is separate class, extension
Problem: Write a program that prompts the user to enter some number of 1,
Problem, simple and annoying. Im just trying to print a list of names, collected
PROBLEM : I am having problems getting socket.IO to connect in some circumstances, so

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.