Trying to create a UserController route where if I create a user it will create empty children tables linked to that user

Is there a way create another Model from inside the route handler of a specific model?

I think I’m most of the way there. Tell me if I’m on the right track as I know you can probably return a tuple of models Future<(User, UserDetails, MatchMakingData)>

and when I save use I can probably access the user.id once it’s done saving by:

let savedUser = try user.save(on: request).flatMap(to: User.self) { user in … }

While inside the closure I can’t seem to figure out how to create a UserDetails, or MatchMaking Model to assign the newly created user.id to it immediately.

@yungdai have a look at the current WebsiteController for the book:

That shows you how to create models when others are created. Just a note, you should only really do this with web clients - with REST these should probably be separate requests

Thank you for the excellent hint, however I believe that’s for chapter 14 of your book which isn’t done yet. I’ll be anxiously waiting for those releases :slight_smile:

1 Like

It’s actually Chapter 16 but yes! :smile:

Thanks for your help though! I was able to create my leaf pages for the API to to create those tables based on your code. It was very helpful.

1 Like