Playing Next Lesson In
seconds

Transcript

  1. So to get our many to many relationships defined for our factories, we'll first want to dive into the factories for each. And we just need to define the relationship

  2. relatively similar for the other relationship types. Do dot relation. We're gonna have an autocomplete come up that offers us our cast members and crew members many to many relationships.

  3. So we'll click on one for that. Do our callback function that returns back the factory for the inverse side of the relationship, which is going to be our Sinist Factory.

  4. And Lucid will automatically recognize that this is a many to many and account for the pivot table when creating records for this relationship. Okay, let's do that exact same thing for our crew members.

  5. So crew members right there, return back our Sinist Factory, just like so. Okay, cool. So let's jump over to our Sinist Factory and do the exact same thing for the inverse side.

  6. So dot relation. Here we have our cast movies and crew movies many to many relationships. So we'll do one here. Return back our Movie Factory.

  7. Again, Lucid's going to be able to use the relationship type to discern how it should create records for this relationship as we use our factories.

  8. And then dot relation, cast movies to create the inverse. Or this one, so Movie Factory, just like so. Awesome. And that's it. The only other difference

  9. is whenever we're actually making use of the factory. So if we jump down to our Fake Cedar, we'll go ahead and start with our straightforward Movie Factory creations. So we have with Director and with Writer,

  10. which will create one individual Sinist for each. In addition to that, we can also do with now, either cast members or crew members. So we'll do cast members first here. And this is where the second argument

  11. really starts to come into play because we get to find a count for the number of records that we want to create. So maybe we want to say that they had 10 cast members a part of this movie. Then we could do that just like so.

  12. Okay, next let's do with our crew members. And it saves that to auto formats. And let's say maybe they had five crew members. So now whenever this Movie Factory runs,

  13. it will create a Writer Sinist, a Director Sinist that was hiding up there, 10 cast members and five crew members. Now we also have that pivot table data

  14. and we can provide a third argument to assign that information. This third argument provides back our factory builder for the relation. So we call this builder,

  15. do our callback function and do builder dot. And you'll see here that this looks relatively similar to just a normal factory. So we could chain another relationship off of it if we wanted to,

  16. but if we look up a little bit further, we'll see our pivot attributes. And that's where we're gonna find the additional pivot table data for each one of these relationships. So if we call this,

  17. we'll see that it can either accept an object for the model or an array of objects for the model. If we provide just an object, whatever we define here for the pivot table data

  18. will be applied to all 10 of the cast members that we're creating. If we instead provide an array of objects, we'll need to provide 10 object items,

  19. one for each cast member that we're creating. And then the object itself would contain the pivot table data for each one of the individual cast members.

  20. So we could do character name, Robert. Let's go ahead and break this down onto a separate line, comma, give that a copy, character name, Joy, Anna,

  21. so on and so forth. And we could do that 10 times. I think maybe for this demonstration, we'll cut this back down to just three cast members. And then we can add in the sort order

  22. for each of these as well. We'll start that at a zero index. So we'll do zero there, sort order one and sort order two. Give that a save to run our formatting.

  23. And there we go. For our crew members, maybe they were all camera operators. So for this builder, we can provide a callback here and do builder.pivotAttributes and provide just a single object

  24. to say that their title was camera operator. Okay, so let's go ahead and attempt to run this. So let's jump back into our terminal. Let's stop our server, clear that out.

  25. Node, ACE, migration, refresh, hyphen, hyphen, seed to roll back, re-migrate and reseed our database. And there we go. So our fake seeder ran successfully.

  26. So we should now have at least three movies with cast and crew members. We jump into pgAdmin. We re-migrated,

  27. so we'll want to scroll up to our database here. Right click, refresh it, scroll back down to our cast movies and crew movies tables. Let's right click our crew movies

  28. and let's view edit data. And let's just click on all rows for right now because it should be relatively minimal. And we see for our crew movies, everybody is a camera operator.

  29. Everybody has a sort order of zero. So nobody was given priority for anything. And we have about five Cineasts per movie ID listed out here

  30. for a total of 15, which would match the three movies that we've created. If we jump over to our cast movies, right click that, view edit data, all rows.

  31. Here we have nine total records. You'll see down here, our sort order goes 012, 012, 012 for the three movies that we have. The IDs for the movies match the same

  32. and it created an individual sentence for each one of these.

Many-To-Many Model Factory Relationships

@tomgobich
Published by
@tomgobich
In This Lesson

We'll learn how we can define and use many-to-many relationships with Model Factories, including how we can define pivot table data when creating our fake records.

Join the Discussion 0 comments

Create a free account to join in on the discussion
robot comment bubble

Be the first to comment!