@@ -15,24 +15,25 @@ const (
1515
1616type 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
2727func 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
3334func 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