[Dbix-class] many to many insert

Jon jon at texttoall.com
Wed Dec 2 16:05:31 GMT 2009


Hello,
I am new to DBIx Class and I am trying to insert a record that
contains a many to many relationship.  I *think* that inserting into
the record should populate the necessary foreign tables, but I can't
get that to work.  Do I need to populate them separately?  That seems
wrong.  This is how I have the relationship setup.

(a many to many relationship between writers and songs)

Package App::Schema::Result::Songs;

__PACKAGE__->has_many('writers_songs',
                      'App::Schema::Result::WritersSongs',
                      { 'foreign.song' => 'self.song' } );
__PACKAGE__->many_to_many('writers' => 'writers_songs','writer');

.....

Package App::Schema::Result::Writers;

__PACKAGE__->has_many('writers_songs',
                      'App::Schema::Result::WritersSongs',
                      { 'foreign.writer' => 'self.writer' } );

__PACKAGE__->many_to_many('songs' => 'writers_songs', 'song');

....

Package App::Schema::Result::WritersSongs;

__PACKAGE__->belongs_to('writer',
                        'App::Schema::Result::Writers',
                        { 'foreign.writer' => 'self.writer' } );

__PACKAGE__->belongs_to('song',
                        'App::Schema::Result::Songs',
                        { 'foreign.song' => 'self.song' } );

...
So, then I try to run

my $song = $schema->resultset('Songs')->find_or_create({
          'artist' => {
                        'artist_name' => 'Beyonce'
                      },
          'writers_songs' => [
                               {
                                 'writer' => {
                                               'name' => 'B.Knowles'
                                             }
                               },
                               {
                                 'writer' => {
                                               'name' => 'J.G.Scheffer'
                                             }
                               },
                               {
                                 'writer' => {
                                               'name' => 'W.Wilkins'
                                             }
                               },
                               {
                                 'writer' => {
                                               'name' => 'Rico Love'
                                             }
                               }
                             ],
          'title' => 'Sweet Dreams',
        });

And it fails to create the writers_songs record, even if the writers
already exist.  I ran a trace to see what is going on, and this is
what I gathered,

It tries to select from writers_songs,
SELECT me.writer, me.song, me.publisher, me.percentage FROM
writers_songs me WHERE ( ( me.song = ? AND me.writer IS NULL ) )

but it never attempts to get the writer foreign key.  SO, when it
tries to insert into writers_songs with a null writer, it fails...

INSERT INTO writers_songs ( song, writer) VALUES ( ?, ? ): '17', 'NULL'

So, the problem appears to be that it never attempts to select from
the writers table, or insert into it in the case where the writer
doesn't yet exist.  Any ideas?

Thanks!

- Jon



More information about the DBIx-Class mailing list