© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
Drizzle TeamDT
Drizzle Team•2y ago•
1 reply
Richard E

Is there a way to set foreign key on a bulk insert?

Apologies if this is a stupid question but I need to know if I'm missing something obvious. I have two tables where the second one has a foreign key relationship to the other, standard master/detail setup.
After I insert the master records, call the entity
Pool
Pool
I return the new
id
id
and then I loop through an array of detail records and set that foreign key
pool_id
pool_id
to the newly acquired
id
id
and then do a batch insert of those detail records
PoolTeam
PoolTeam
like so
async createPoolTeamPlayers(input: CreatePoolTeamPlayer[]): Promise<void> {
    await db.insert(pool_team_player).values(input)
}
async createPoolTeamPlayers(input: CreatePoolTeamPlayer[]): Promise<void> {
    await db.insert(pool_team_player).values(input)
}


Is there any way I can specify the master record
id
id
in this insert statement without having to previous loop through the array setting the
pool_id
pool_id
property? It it possible to do this master/detail insert in a single call with Drizzle? It works the way I have it but just seems wrong to do it this way.
Drizzle TeamJoin
The official Discord for all Drizzle related projects, such as Drizzle ORM, Drizzle Kit, Drizzle Studio and more!
11,879Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Ignore foreign key violation on bulk insert
Drizzle TeamDTDrizzle Team / help
2y ago
ON DUPLICATE KEY UPDATE with bulk insert?
Drizzle TeamDTDrizzle Team / help
3y ago
Foreign Key - On Delete
Drizzle TeamDTDrizzle Team / help
3y ago
foreign key
Drizzle TeamDTDrizzle Team / help
3y ago