bliako has asked for the wisdom of the Perl Monks concerning the following question:
I have posted this on so a couple of hours ago asking about the name of the problem I am facing but ... "computer says no" (yt: https://www.youtube.com/watch?v=e3MZsirJeqI). So here is the same question re-shaped for DBIx::Class which is my current setting.
DBIx::Class (DBIC) offers belongs_to and has_many functionality. However, I need to adjust these for this situation and I am looking for ideas before re-inventing or damaging the wheel:
A Comment table contains comments made by User on either a Concert or Play or Album or Painting, etc.
I am trying to avoid using many different comment tables like CommentPlay, CommentAlbum while I want to search for $schema->resultset('Painting')->find(id=>$paintid)->comments().
Adding a column like comment_type in the Comment table is OK and that was my initial thought, but how do I tell DBIC the context when calling comments() above?
Even having the name of this kind of situation can help me look for it further, the so question attracted a comment citing "Table Inheritance" but that sounds way too complex to my sql-illiterate mind.
Edit: added some example schemas
package MyApp::Schema::Result::Comment; use base qw/DBIx::Class::Core/; __PACKAGE__->table('comment'); __PACKAGE__->add_columns( id => {data_type => 'integer',is_auto_increment => 1,}, # e.g. comment for Play, or Concert, example value: "play" context => {data_type => 'text'}, ); __PACKAGE__->belongs_to ( # XXX can be Play or Concert or ... parent => 'MyApp::Schema::Result::XXX', 'YYY' # FK_id? for ids of Play or Concert or ...? );
package MyApp::Schema::Result::Play; use base qw/DBIx::Class::Core/; __PACKAGE__->table('play'); __PACKAGE__->add_columns( id => {data_type => 'integer',is_auto_increment => 1,}, context => {data_type => 'text'}, ); __PACKAGE__->has_many( comments => 'MyApp::Schema::Result::Comment', 'XXX' # what can go here? shall I add a FK_id in "comment"? );
Similarly a MyApp::Schema::Result::Concert, a MyApp::Schema::Result::Painting etc.
|
|---|