[英]How to setup a multi primary column 1:n relationship with DBIx::Class in Perl?
我正在用DBIx :: Class :: Candy自己編寫DBIx :: Class模式類。 目前,我在復雜數據庫設計的某些部分腳。
我已經使用DBIx :: Class :: Relationship作為模板來設置大多數類,以對1:n
, n:1
和n:m
關系(使用一個主鍵)進行建模。 很好,但是我沒有運行這種特殊的關系。
Game_Users
是兩個不相關的表Games
和Users
之間的n:m
關系表。 Turns
,另一方面是一個“正常”表,其存儲用於游戲和用戶的組合單匝。
Game_Users
包名稱為MyApp::Schema::Result::GameUser
Turns
名稱為MyApp::Schema::Result::Turn
如何在Perl中設置與DBIx :: Class的多主列1:n關系?
我想指出的是,即使我在此處顯示了一個具體示例,該通用問題也可能會引起大量讀者的關注。
您可以簡單地為hashref提供具有has_many
和belongs_to
的聯接表達式。 在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',
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.