[Dbix-class] foreign keys in fixtures for Test::DBIx::Class
Ben Tilly
btilly at gmail.com
Thu Nov 20 19:12:38 GMT 2014
In the documentation there is the following example:
fixtures_ok my $first_album = sub {
my $schema = shift @_;
my $cd_rs = $schema->resultset('CD');
return $cd_rs->create({
name => 'My First Album',
track_rs => [
{position=>1, title=>'the first song'},
{position=>2, title=>'yet another song'},
],
cd_artist_rs=> [
{person_artist=>{person => $vanessa}},
{person_artist=>{person => $john}},
],
});
}, 'You can even use a code reference for custom fixtures';
This creates a 'CD' object with a list of tracks, and two entries in a
join table that refer to artists who had been created separately.
You should be able to do the same thing. Create a company, with a
result set that contains the people who are part of that company.
On Thu, Nov 20, 2014 at 7:06 AM, Adam Witney <awitney at sgul.ac.uk> wrote:
> Hi,
>
> I am trying to write some fixtures for testing with Test::DBIx::Class. I can do this for a simple one table test, but am having trouble doing it when I have a foreign key between two tables. In the file "t/etc/schema.pl" I have
>
> fixture_sets => {
> 'basic' => [
> {
> Person => [
> [ 'first_name', 'last_name', ],
> [ 'John', 'Smith' ],
> ]
> },{
> Company => [
> ['name'],
> ['My Company']
> ]
> }
> ]
> }
>
> But I cannot figure out how to assign the Company to Person John Smith. Does anyone have a working example of this syntax?
>
> The becomes a problem when using foreign keys in PostgreSQL, as it can't create the Person row without an id for the company row
>
> Thanks for any help
>
> Adam
>
> _______________________________________________
> List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
> IRC: irc.perl.org#dbix-class
> SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
> Searchable Archive: http://www.grokbase.com/group/dbix-class@lists.scsys.co.uk
More information about the DBIx-Class
mailing list