Hello,
I'm trying to insert data into a table that has a one-to-many relation with another table, preferably in one SQL query.
For a more concrete example, let's say we have a Channels that has many Videos. So when inserting a new video I want to associate it with a specific channel without knowing the channel id (which is the foreign key).
I could do it like this, which is perfectly fine except it involves two SQL queries:
Channel.findOne({ name: 'My Channel' })
.then(function (channel) {
channel.addVideo(Video.build({ name: 'My Video' }));
});
Do you know of another way to achieve this?
I noticed that Model.create() has an include option, is that intended for these kind of scenarios? If so, do you have an example of how it would be used?
Thanks.