How to do a foreign key with cascades

Right now I'm doing something like this:
mysqlTable('users',
{
id: varchar('id', { length: 36 }).notNull().default('uuid-will-be-generated').primaryKey(),
avatarId: varchar('avatarId', { length: 36 }).default(null).references(() => S3FileTable.id, { onDelete: "set null", onUpdate: "no action" })
},
);
mysqlTable('users',
{
id: varchar('id', { length: 36 }).notNull().default('uuid-will-be-generated').primaryKey(),
avatarId: varchar('avatarId', { length: 36 }).default(null).references(() => S3FileTable.id, { onDelete: "set null", onUpdate: "no action" })
},
);
I would like to name the key that is generated here. I understand that I can use the foreignKey() to name the key. However, I don't know how to handle the cascades if I do the foreign key using that function rather than using references(). Can anybody give me a pointer?
2 Replies
Aaroned
Aaroned9mo ago
@JT I haven't tested this, but what about:
table => ({
avatarId_foreignKey: foreignKey({
name: 'avatarId_foreignKey',
columns: [table.avatarId],
foreignColumns: [S3FileTable.id],
}).onDelete('set null').onUpdate('no action'),
})
table => ({
avatarId_foreignKey: foreignKey({
name: 'avatarId_foreignKey',
columns: [table.avatarId],
foreignColumns: [S3FileTable.id],
}).onDelete('set null').onUpdate('no action'),
})
JT
JTOP9mo ago
Genius! Thank you. That totally worked.
Want results from more Discord servers?
Add your server