Skip to content

Commit 2949278

Browse files
authored
Merge pull request #7 from syucream/mitigate-keycheck
Allow non-text/blob columns as a foreign key
2 parents 9698c37 + 858b8a3 commit 2949278

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

src/guess/guess.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,25 @@ const (
1515

1616
type GuessOption func(database.Column, string, database.Column) bool
1717

18-
func isAcceptableAsPrimaryKey(columnType, primaryKeyType string) bool {
19-
colIsOk := strings.Index(columnType, "int") != -1
20-
pkIsOk := strings.Index(primaryKeyType, "int") != -1
21-
return colIsOk && pkIsOk && columnType == primaryKeyType
18+
func isAcceptableAsIndex(left, right string) bool {
19+
return left == right &&
20+
!(strings.Index(left, "text") != -1 || strings.Index(left, "blob") != -1) &&
21+
!(strings.Index(right, "text") != -1 || strings.Index(right, "blob") != -1)
2222
}
2323

2424
// Recongnize a column thats same name of other table's primary key is a foreign key
2525
// This base idea refers to SchemaSpy DbAnalyzer:
2626
// https://github.com/schemaspy/schemaspy/blob/master/src/main/java/org/schemaspy/DbAnalyzer.java
2727
func GuessByPrimaryKey() GuessOption {
2828
return func(i database.Column, table string, pk database.Column) bool {
29-
return isAcceptableAsPrimaryKey(i.Type, pk.Type) && i.Name == pk.Name && pk.Name != idColumn
29+
return isAcceptableAsIndex(i.Type, pk.Type) && i.Name == pk.Name && pk.Name != idColumn
3030
}
3131
}
3232

33+
// Recongnize a column thats same name without '_id' suffix of other table name is a foreign key
3334
func GuessByTableAndColumn() GuessOption {
3435
return func(i database.Column, table string, pk database.Column) bool {
35-
if !isAcceptableAsPrimaryKey(i.Type, pk.Type) {
36+
if !isAcceptableAsIndex(i.Type, pk.Type) {
3637
return false
3738
}
3839

0 commit comments

Comments
 (0)