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 682479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:36:01+00:00 2026-05-14T01:36:01+00:00

In my code I have three classes as follows: Forum , Forum::Thread and Forum::Post

  • 0

In my code I have three classes as follows: Forum, Forum::Thread and Forum::Post

What I want to do is create a belongs_to-relationship from the Forum::Post class to the Forum class and vice versa with a has_many, preferably without creating a custom function for it. (This is admittedly more of an intellectual exercise than a technical limitation or actual problem, but if it is possible, I would much like to know.)

The commented out lines contain my intention with the relationships, but in their current form, they fail to work. I’ve poked around in the documentation, but cannot find anything relevant to this specific case.

Any pointers?

The forum class:

package Schema::Result::Forum;

use Moose;
extends qw/DBIx::Class/;

__PACKAGE__->load_components (qw/Core/);
__PACKAGE__->table ('forum');

__PACKAGE__->add_columns (
    id => {
    is_auto_increment => 1,
    data_type         => 'integer',
  },
);

__PACKAGE__->set_primary_key ('id');

__PACKAGE__->has_many (threads => 'Schema::Result::Forum::Thread');
#This is the interesting line
#__PACKAGE__->has_many (posts => 'threads' => 'forums' );

1;

The thread class:

package Schema::Result::Forum::Thread;

use Moose;
extends qw/DBIx::Class/;

__PACKAGE__->load_components (qw/Core/);
__PACKAGE__->table ('forum_thread');
__PACKAGE__->add_columns (
  id => {
    is_auto_increment => 1,
    data_type         => 'integer',
  },
  forum => {
    data_type         => 'integer',
  },
);

__PACKAGE__->set_primary_key ('id');

__PACKAGE__->belongs_to (forum => 'Schema::Result::Forum');
__PACKAGE__->has_many (posts => 'Schema::Result::Forum::Post');

1;

The post class:

package Schema::Result::Forum::Post;

use Moose;

extends qw/DBIx::Class/;

__PACKAGE__->load_components (qw/Core/);

__PACKAGE__->table ('forum_post');

__PACKAGE__->add_columns (
  id => {
    is_auto_increment => 1,
    data_type         => 'integer',
  },
  thread => {
    data_type         => 'integer',
  },
);

__PACKAGE__->set_primary_key ('id');

__PACKAGE__->belongs_to (thread => 'Schema::Result::Forum::Thread');
#This is the other interesting line
#__PACKAGE__->belongs_to (forum => 'thread' => 'forum');

1;

PS: Additional columns to hold actual content were omitted for brevity.

  • 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-05-14T01:36:02+00:00Added an answer on May 14, 2026 at 1:36 am

    It looks like nested relationships aren’t possible. has_many takes a single foreign class, which has a foreign key to the calling class.

    The good news is $forum->threads->posts returns a single DBIx::Class::Resultset. It is not translated into SQL until needed, so when you call $forum->threads->posts->all() or even something like $forum->search_related('threads',{},{rows=>25})->posts->all(), it only runs a single query.

    If your goal is to have a $post->forum, it can always be a method: sub forum{$_[0]->thread->forum}

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have code like that 010200345 i want split every first three digit please
I have written this code inside a servlet to delete certain records from three
I have the simplest code that I want to separate in three files: Header
I have two classes defined as follows. First one: internal class Content { internal
I have this code and it always returns -1.I have three tables (a picture
Task at hand — I have three versions of some code, developed by different
I have the following code string three() { return three; } void mutate(string& ref)
I have an issue in entity framework code first, three table in database as
Have there been incompatibilities between Java releases where Java source code/Java class files targeting
I have inherited an existing code base where the features are as follows: huge

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.