Question

I'm writing DBIx::Class schema classes by myself with DBIx::Class::Candy. Currently I stumple around some part of an complexe database design.

I have setup most of the classes using DBIx::Class::Relationship as template for modelling the 1:n, n:1 and n:m relationships (with one single primary key). All fine but I don't get this particular relationship running.

Game_Users is an n:m relation table between the two irrelevant tables Games and Users. Turns on the other hand is a "normal" table which stores single turns for the combination of game and user.

  • Game_Users package name is MyApp::Schema::Result::GameUser
  • Turns package name is MyApp::Schema::Result::Turn

How to setup this multi primary column 1:n relationship with DBIx::Class in Perl?

I like to point out that even if I show a specific example here, the generic question may be of interest by a large number of audience.

Database desing: Game_Users to Turns relation

Was it helpful?

Solution

You can simply supply a hashref with the join expression to has_many and belongs_to. In MyApp::Schema::Result::GameUser:

__PACKAGE__->has_many(turns => 'MyApp::Schema::Result::Turn', {
    'foreign.game_id' => 'self.game_id',
    'foreign.user_id' => 'self.user_id',
});

In MyApp::Schema::Result::Turn:

__PACKAGE__->belongs_to(game_user => 'MyApp::Schema::Result::GameUser', {
    'foreign.game_id' => 'self.game_id',
    'foreign.user_id' => 'self.user_id',
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top