From 503e1356e1262574d7fae0b229dac2e1ba4236bb Mon Sep 17 00:00:00 2001 From: Jim Lambert Date: Wed, 12 Feb 2020 12:01:46 -0500 Subject: [PATCH] clean up oracle foreign key bits --- scope.go | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/scope.go b/scope.go index 409f2f3c..bfc442b3 100644 --- a/scope.go +++ b/scope.go @@ -8,6 +8,7 @@ import ( "fmt" "reflect" "regexp" + "sort" "strings" "time" ) @@ -1234,7 +1235,11 @@ func (scope *Scope) addForeignKey(field string, dest string, onDelete string, on if scope.Dialect().HasForeignKey(scope.TableName(), keyName) { return } - var query = `ALTER TABLE %s ADD CONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s ON DELETE %s ON UPDATE %s;` + if scope.IsOracle() { + scope.Raw(fmt.Sprintf(`ALTER TABLE %s ADD CONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s`, scope.QuotedTableName(), scope.quoteIfPossible(keyName), scope.quoteIfPossible(field), dest)).Exec() + return + } + var query = `ALTER TABLE %s ADD CONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s ON DELETE %s ON UPDATE %s` scope.Raw(fmt.Sprintf(query, scope.QuotedTableName(), scope.quoteIfPossible(keyName), scope.quoteIfPossible(field), dest, onDelete, onUpdate)).Exec() } @@ -1420,3 +1425,12 @@ func (scope *Scope) hasConditions() bool { len(scope.Search.orConditions) > 0 || len(scope.Search.notConditions) > 0 } + +func (scope *Scope) IsOracle() bool { + oraModules := []string{"godror", "oci8", "ora"} // must be an asc sorted slice + insertAt := sort.SearchStrings(oraModules, scope.Dialect().GetName()) + if insertAt < len(oraModules) && oraModules[insertAt] == scope.Dialect().GetName() { + return true + } + return false +}