Oracle Foreign Key Constraints: ON DELETE and ON UPDATE
Problem Description
Oracle behaviour:
-
ON UPDATE: Oracle does not support any
ON UPDATEactions for foreign keys -
ON DELETE: Oracle only supports
CASCADEandSET NULLactions
Currently, Bun sets NO ACTION as the default for both OnUpdate and OnDelete in foreign key relations, which causes SQL errors when working with Oracle databases.
Expected Behavior
When using Oracle dialect, Bun should:
- Automatically set
OnUpdateto empty/null (since Oracle doesn't support it) - Only allow
CASCADEandSET NULLforOnDeleteactions - Either throw a validation error or automatically clear unsupported
OnDeletevalues
Current Behavior
Bun generates foreign key constraints with NO ACTION for both OnUpdate and OnDelete, which fails on Oracle with errors like:
[!CAUTION] ORA-02000: missing CASCADE keyword
Example Code
Model Definition
type User struct {
ID int64 `bun:",pk,autoincrement"`
Name string
Emails []string
}
type Story struct {
ID int64 `bun:",pk,autoincrement"`
Title string
AuthorID int64
Author *User `bun:"rel:belongs-to,join:author_id=id"`
}
Generated SQL (Current - Fails on Oracle)
CREATE TABLE "stories" ("id" INTEGER GENERATED BY DEFAULT AS IDENTITY, "title" VARCHAR2(255), "author_id" INTEGER, PRIMARY KEY ("id"), FOREIGN KEY ("author_id") REFERENCES "users" ("id") ON UPDATE NO ACTION ON DELETE NO ACTION
Thank you for your feedback and the solution you provided. It’s very detailed, and I basically don’t need to look up any additional information.
In the related PR, I attempted to provide default values for each dialect. However, I deferred the check for user-defined values until SQL execution, as I believe such errors are extremely rare.