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?)
1 Reply
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()