From 858b8a3de63cc05f5cf275862bdfc246bf485732 Mon Sep 17 00:00:00 2001 From: Ryo Okubo Date: Tue, 16 Oct 2018 01:14:14 +0900 Subject: [PATCH] Allow non-text/blob columns as a foreign key --- src/guess/guess.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/guess/guess.go b/src/guess/guess.go index 04d6ca6..1cf0afb 100644 --- a/src/guess/guess.go +++ b/src/guess/guess.go @@ -15,10 +15,10 @@ const ( type GuessOption func(database.Column, string, database.Column) bool -func isAcceptableAsPrimaryKey(columnType, primaryKeyType string) bool { - colIsOk := strings.Index(columnType, "int") != -1 - pkIsOk := strings.Index(primaryKeyType, "int") != -1 - return colIsOk && pkIsOk && columnType == primaryKeyType +func isAcceptableAsIndex(left, right string) bool { + return left == right && + !(strings.Index(left, "text") != -1 || strings.Index(left, "blob") != -1) && + !(strings.Index(right, "text") != -1 || strings.Index(right, "blob") != -1) } // Recongnize a column thats same name of other table's primary key is a foreign key @@ -26,13 +26,14 @@ func isAcceptableAsPrimaryKey(columnType, primaryKeyType string) bool { // https://github.com/schemaspy/schemaspy/blob/master/src/main/java/org/schemaspy/DbAnalyzer.java func GuessByPrimaryKey() GuessOption { return func(i database.Column, table string, pk database.Column) bool { - return isAcceptableAsPrimaryKey(i.Type, pk.Type) && i.Name == pk.Name && pk.Name != idColumn + return isAcceptableAsIndex(i.Type, pk.Type) && i.Name == pk.Name && pk.Name != idColumn } } +// Recongnize a column thats same name without '_id' suffix of other table name is a foreign key func GuessByTableAndColumn() GuessOption { return func(i database.Column, table string, pk database.Column) bool { - if !isAcceptableAsPrimaryKey(i.Type, pk.Type) { + if !isAcceptableAsIndex(i.Type, pk.Type) { return false }