Table foreign key action fields

Hello, I'd like to use the new foreignKey option inside the table config so that I do not have issues with the identifier being longer than allowed. Is there a way to achieve this whilst also setting the onCascade and onUpdate action properties like you could previously with the .references() syntax? I tried leaving both of them in place (like in the code below) with the hope that it would only generate one foreign key with my name provided and the actions I set with references() but it generates two constraints in the SQL migration file, one with my custom identifier and no actions and one with the default generated constraint name (too long for my DB) and with the actions. I didn't really expected this to work but It would be good to know the best approach going forward to set the custom FK name and also set the actions. Perhaps it is something missed with the latest update for the new foreignKey() functionality? (I could always just change the migration file manually, like I had previously been doing for the foreign key names which were too long but they kept coming back in future migrations with the default generated name, presumably because the _journal.json files have references to the generated fk names which I had overwritten?)
export const testTable = mysqlTable(
'test_table',
{
id: int('id').autoincrement().primaryKey(),
userId: int('user_id')
.notNull()
.references(() => users.id, { onDelete: 'cascade', onUpdate: 'cascade' }),
},
(table) => {
return {
userFk: foreignKey({
name: 'test_table_user_id_fk',
columns: [table.userId],
foreignColumns: [users.id],
}),
};
},
);
export const testTable = mysqlTable(
'test_table',
{
id: int('id').autoincrement().primaryKey(),
userId: int('user_id')
.notNull()
.references(() => users.id, { onDelete: 'cascade', onUpdate: 'cascade' }),
},
(table) => {
return {
userFk: foreignKey({
name: 'test_table_user_id_fk',
columns: [table.userId],
foreignColumns: [users.id],
}),
};
},
);
1 Reply
T.T
T.TOP13mo ago
For anyone reading this in the future: You can provide onUpdate and onDelete actions as functions which you apply to the foreignKey function within your table config. They aren't part of the arguments of the foreignKey functions like they previously were for the references use case. Such as: Fk: foreignKey({...}).onUpdate().onDelete()
Want results from more Discord servers?
Add your server