When using a foreignKey({}), is there a way to specify onDelete: "cascade'?

I'm looking to custom name my foreign keys so I need to use the foreignKey() function. However, how can I specify an action: {onDelete: "cacade"} using this function?
userIdReference: foreignKey(
{
columns: [table.userId],
foreignColumns: [users.userId],
name: "accounts_user_id_fk",
},
),
userIdReference: foreignKey(
{
columns: [table.userId],
foreignColumns: [users.userId],
name: "accounts_user_id_fk",
},
),
To my understanding, if I use the .references() method, it doesn't allow for naming foreign keys
userId: integer('user_id').references(() => users.id, { onDelete: 'cascade' })
userId: integer('user_id').references(() => users.id, { onDelete: 'cascade' })
3 Replies
Mykhailo
Mykhailo6mo ago
Hey @Paul. Try this
userIdReference: foreignKey({
columns: [table.userId],
foreignColumns: [users.userId],
name: "accounts_user_id_fk",
})
.onDelete("cascade")
// .onUpdate("cascade"),
userIdReference: foreignKey({
columns: [table.userId],
foreignColumns: [users.userId],
name: "accounts_user_id_fk",
})
.onDelete("cascade")
// .onUpdate("cascade"),
Paul
PaulOP6mo ago
Thanks a lot. This works. I think adding this to the documentation would be helpful to avoid this question forever since the AI in the docs is getting it wrong currently.
Mykhailo
Mykhailo6mo ago
Yep, we will add it to the docs, thanks!
Want results from more Discord servers?
Add your server