【问题标题】:How to setup a multi primary column 1:n relationship with DBIx::Class in Perl?如何在 Perl 中设置与 DBIx::Class 的多主列 1:n 关系?
【发布时间】:2013-08-17 14:57:54
【问题描述】:

我自己用DBIx::Class::Candy 编写DBIx::Class 模式类。目前,我在复杂数据库设计的某些部分遇到了困难。

我已经使用DBIx::Class::Relationship 作为模板设置了大多数类,用于建模1:nn:1n:m 关系(使用一个主键)。一切都好,但我没有得到这种特殊的关系。

Game_Usersn:m 两个不相关的表GamesUsers 之间的关系表。另一方面,Turns 是一个“普通”表,用于存储游戏和用户组合的单回合。

  • Game_Users 包名是MyApp::Schema::Result::GameUser
  • Turns 包名是MyApp::Schema::Result::Turn

如何在 Perl 中使用 DBIx::Class 设置这种多主列 1:n 关系?

我想指出,即使我在这里展示一个具体的例子,一般性的问题也可能会引起大量观众的兴趣。

【问题讨论】:

    标签: perl relationship dbix-class


    【解决方案1】:

    您可以简单地为has_manybelongs_to 提供一个带有连接表达式的hashref。在MyApp::Schema::Result::GameUser

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

    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',
    });
    

    【讨论】:

      猜你喜欢
      • 2016-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-09
      • 1970-01-01
      • 2013-01-02
      • 2023-03-18
      相关资源
      最近更新 更多