Playing Next Lesson In
seconds

Transcript

  1. >> So ideally, every user that we have inside of our application is going to have a profile as well. That's going to ring true for the users that we're creating inside of our fake seeder as well.

  2. So what we can do to define that is, we have both a profile and a user factory. What we'll want to do is define the relationship inside of this factory as well,

  3. and then we can make use of that relationship inside of our fake seeder so that anytime that we create a user, we can create a relationship record for that created user automatically just

  4. using the relationship that we've defined on the factory itself. So for example, similar to how on our movie profile we're defining states, we can chain off of the define method,

  5. an additional method called relation. The first argument to this relation method is going to be the name of the relationship. If we hit our strings there, we'll see that we have a profile option at the ready because it's

  6. extracting model relations from our user model. So we can go ahead and fill that out with our profile. Then the second argument to this relation call is going to be a callback function.

  7. This callback method is going to need to return the related factory. So in this case, that will be our profile factory. We could just return that variable. We don't need to do anything with it, and that's it.

  8. So now we've defined that our user factory has a relation of profile that should use the profile factory anytime that we want to generate out a relationship for our user.

  9. Let's go ahead and take care of the inverse side of that as well. So we'll jump over to our profile factory.relationUserFactory, and there we go. Now, it's worth noting that you will want your relationship to

  10. exist on the model before you start defining your relationships inside of your factories, because that's what this relation key right here in the first argument is going to populate from. So now with our factory relationships defined,

  11. we can jump down to our fake seeder where we're creating five users making use of our user factory. Well, with this user factory, we can specify that we want to create it with the profile relationship.

  12. By default, this with method will create just one relationship. But if you need multiple, the second argument, you can provide a count for the number of relations that should be created.

  13. However, our user profile is a one-to-one, so we'll only want one here. Let's give that a save. Let's go ahead and hide our text editor away, stop our server, clear that out.

  14. Let's do node is migration refresh, hyphen, hyphen, seed to roll back, re-migrate, and re-seed everything. There we go.

  15. Awesome. Let's go ahead and open back up PG Admin. We'll dive into our servers, Postgres server that we're working with. We'll dive into our Adonis 6 database down to tables,

  16. and let's right-click our users and view edit data. We can just do all rows right now, it should just be five. So we have our five user records right here with an ID of 1, 2, 3, 4, and 5.

  17. Let's go ahead and right-click on our profiles. What we would expect to see here is five profile records with user IDs 1, 2, 3, 4, and 5,

  18. defining that there's a relationship there for these records. We see exactly that. We have our user ID column right here with the values 1, 2, 3, 4, and 5,

  19. meaning that this row right here is related to our user with an ID of 1. This row right here is related to our user with an ID of 2, 3, 4, and 5, and so on and so forth,

  20. as we continue to create records inside of both of these tables.

Model Factory Relationships

In This Lesson

We'll learn how to use relationships with our Model Factories; easing our capabilities to quickly generate fake data with relationships.

Created by
@tomgobich
Published

Join the Discussion 0 comments

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

Be the first to comment!