We want to practice one-to-many relationships similar to what we've seen in the Hello Books API. To do this we will:
- Add a Moon model
- Create a one-to-many relationship between the Planet and Moon models
- Add nested routes for the endpoint
/planets/<planet_id>/moonsto:- Create a Moon and link it to an existing Planet record
- Fetch all Moons that a Planet is associated with
- Create a Moon model with attributes
sizeanddescription, and at least one other attribute you choose together. - Make a foreign key connection between Planet and Moon models using
back_populatesto create a bidirectional relationship. SQLAlchemy docs on one-to-many- Make sure to create and run migrations as the models change, and to commit those files so your teammates can access them as well!
- For testing, you may want to update the Planet’s
to_dictfunction to return info about their moons, if they exist.
- Create a nested route for
/planets/<planet_id>/moonsusing the POST method which allows you to add a new moon to an existing planet resource with id<planet_id>.- Make sure to test out your new route in a browser or Postman before continuing!
- Create a nested route for
/planets/<planet_id>/moonswith the GET method which returns all moons for the planet with the id<planet_id>.
If you finish and have more time, take a look at what else could be refactored or discuss what other nested endpoints you think would be helpful!