T.T
T.T
Explore posts from servers
DTDrizzle Team
Created by T.T on 11/10/2023 in #help
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],
}),
};
},
);
2 replies