[Dbix-class] Populate and related tables

Louis Erickson lerickson at rdwarf.net
Sun May 11 02:38:22 GMT 2014


I've been throwing away some old code and replacing it with DeploymentHandler.  All my old hand-written SQL files are being replaced by Perl which uses DBIC to populate the database.

One of those functions to populate the database didn't work right.  Eventually, I boiled it down to an standalone test, which still fails in a way I didn't expect.  I'm not sure if I'm doing something wrong, or if I've missed something in the docs, or if there's a lurking bug.

What I'm seeing is that if I call populate() with a data structure where the first item does not reference other tables, none of the other items in the data structure will create items in referenced tables.  If the first item does refer to other tables, all other references will get created as I expected.

My sample has two simple tables:

package TestDb::Schema::Result::Artist;

use Moose;
use MooseX::NonMoose;
use MooseX::MarkAsMethods autoclean => 1;
extends 'DBIx::Class::Core';

__PACKAGE__->table ('artist');

__PACKAGE__->add_columns (
  id => {
    data_type => 'int',
    is_auto_increment => 1,
  },
  name => {
    data_type => 'varchar',
  },
);

__PACKAGE__->set_primary_key ('id');

__PACKAGE__->has_many (cds => 'TestDb::Schema::Result::CD', 'artist_id');

__PACKAGE__->meta->make_immutable;

1;

package TestDb::Schema::Result::CD;
 
use Moose;
use MooseX::NonMoose;
use MooseX::MarkAsMethods autoclean => 1;
extends 'DBIx::Class::Core';
 
__PACKAGE__->table ('cd');
 
__PACKAGE__->add_columns (   
  id => {
    data_type => 'int',
    is_auto_increment => 1,
  },
  title => {
    data_type => 'varchar',
  },
  artist_id => {
    data_type => 'int',
  },
);
 
__PACKAGE__->set_primary_key ('id');
 
__PACKAGE__->belongs_to (artist => 'TestDb::Schema::Result::Artist', 'artist_id');

__PACKAGE__->meta->make_immutable;

1;

Simple as I could make it.

When I call populate with data like this, everything works:

$schema->resultset('Artist')->populate([
	{
		name => 'The Beatles',
		cds => [
			{ title => "Sgt. Pepper's Lonely Hearts Club Band" },
			{ title => "Yellow Submarine" },
			{ title => "Rubber Soul" },
			{ title => "Meet the Beatles" },
		],
	},
	{
		name => 'ABBA',
		cds => [
			{ title => 'ABBA Gold' },
			{ title => 'More ABBA Gold '},
		],
	},
	{ name => 'Anonymous' },
]);

This creates an artist called "The Beatles", and CDs called "Sgt. Pepper's Lonely Hearts Club Band", "Yellow Submarine", "Rubber Soul", and "Meet the Beatles", all referring to the right artist_id for the newly-created "The Beatles" artist.  An artist called ABBA is created, as are both of those greatest hits CDs.  An an "Anonymous" artist is created with no CDs.

Perfect!

But, if I move the {name => 'Anonymous}, first, before any of the others, the behavior is quite different.  None of the CDs get created, just the artists.  There's no errors, no complaints, just no entries in the related tables.

Not what I expected.

Can anyone shed some light on this?  Any thoughts are appreciated!

I have full working code if someone wants me to put a tar file somewhere.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.scsys.co.uk/pipermail/dbix-class/attachments/20140510/38bc8f8f/attachment.htm>


More information about the DBIx-Class mailing list