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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T18:12:45+00:00 2026-05-13T18:12:45+00:00

Let’s suppose I have a the following simplified example database consisting of three tables:

  • 0

Let’s suppose I have a the following simplified example database consisting of three tables:

CREATE TABLE people (
    person_id   INTEGER PRIMARY KEY,
    person_name VARCHAR(100)
);

CREATE TABLE events (
    event_id       INTEGER PRIMARY KEY,
    event_name     VARCHAR(100),
    event_creator  INTEGER
                   CONSTRAINT fk_event_creator REFERENCES people(person_id)
);

CREATE TABLE event_attendees (
    event_id  INTEGER NOT NULL
              CONSTRAINT fk_event_attendee_event
              REFERENCES events(event_id),
    person_id INTEGER NOT NULL
              CONSTRAINT fk_event_attendee_person
              REFERENCES people(person_id),
    role      CHAR(1), -- O: organizer, P: performer, S: speaker, G: guest
    CONSTRAINT pk_event_attendees PRIMARY KEY (event_id, person_id)
);

Given an event_id, I might want to query for the names of all organizers, given a person_id I might want to find names of all events where this person is a guest or creator of the event so on and so forth.

I know how to do all that using simple SQL. Could you tell me which result classes I need to set up and what kinds of relationships I need to specify when using DBIx::Class?

  • 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-13T18:12:45+00:00Added an answer on May 13, 2026 at 6:12 pm

    Are you familiar with DBIx::Class::Schema::Loader? Although it can be used in one-off scripts to create a DBIC schema dynamically in memory, it also has the ability to run in a “one-shot” mode where it writes the schema definitions to disk for you to edit and build on, and it’s way more advanced than you might think.

    First, you want to have the schema actually present in a database, so that the loader can scan it. Then you do something like:

    perl -MDBIx::Class::Schema::Loader=make_schema_at \
    -e 'make_schema_at("MyApp::Schema", {dump_directory=>"schema_out"},' \
    -e '["dbi:DBType:connstring", "user", "pass"]);'
    

    (where “MyApp::Schema” is the package name you want the generated schema classes to share, and “schema_out” is the directory you want them to be generated in).

    After this, you can either edit the generated schema classes, or, if you find that the loader does a good enough job (or at least a good enough job that you don’t need to edit anything above the “DON’T EDIT ABOVE THIS LINE” line), you can decide that the schema in the DB is your primary source, and save the Schema::Loader script to be run again to re-generate the classes automatically if the DB changes.

    Update

    Parts of the above schema don’t get processed correctly with DBIx::Class::Schema::Loader v0.05002 because Sinan managed to find a bug! Foreign key constraints weren’t parsed correctly if the “references” part and the column name weren’t on the same line.

    The bug is fixed in DBICSL git, but since the fix isn’t released yet, here’s what the relations should look like (I’m leaving out the column definitions to save space; they should be just as the loader currently generates them).

    EventAttendee.pm

    __PACKAGE__->set_primary_key(qw(event_id person_id));
    
    __PACKAGE__->belongs_to(
        "event" => "MyApp::Schema::Result::Event",
        { event_id => "event_id" },
        {}
    );
    
    __PACKAGE__->belongs_to(
        "person" => "MyApp::Schema::Result::Person",
        { person_id => "person_id" },
        {}
    );
    

    Event.pm

    __PACKAGE__->set_primary_key("event_id");
    
    __PACKAGE__->belongs_to(
        "event_creator" => "MyApp::Schema::Result::Person",
        { person_id => "event_creator" },
        { join_type => "LEFT" },
    );
    
    __PACKAGE__->has_many(
        "event_attendees" => "MyApp::Schema::Result::EventAttendee",
        { "foreign.event_id" => "self.event_id" },
    );
    
    # Not auto-generated, but you probably want to add it :)
    __PACKAGE__->many_to_many(
        "people_attending" => "event_attendees" => "person"
    );
    

    People.pm

    __PACKAGE__->has_many(
        # It might be wise to change this to "events_created"
        "events" => "MyApp::Schema::Result::Event",
        { "foreign.event_creator" => "self.person_id" },
    );
    
    __PACKAGE__->has_many(
        "event_attendees" => "MyApp::Schema::Result::EventAttendee",
        { "foreign.person_id" => "self.person_id" },
    );
    
    # Not auto-generated, but you probably want to add it :)
    __PACKAGE__->many_to_many(
        "events_attending" => "event_attendees" => "event"
    );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

let's say i have three tables, each one relates to another, when i need
Let's say I have the following, declare @A table (a int) insert into @A
Let's say you have a class called Customer, which contains the following fields: UserName
Let's say I have a link in a table like: <td class=ms-vb width=100%> <a
Let's say I have a table that looks something like this: ------------------------------- id|column2|column3 |column4
Let say I have the following desire, to simplify the IConvertible's to allow me
Let's say I have the following code: Sub TestRangeLoop() Dim rng As Range Set
Let's say I have the following function in C#: void ProcessResults() { using (FormProgress
Let's say I have the following within my source code, and I want to
Let's suppose I have a trait with two type parameters, e.g. trait Qux[A, B]

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.