Skip to content

Commit be29cf0

Browse files
author
syucream
committed
Improve column and primary key checking
1 parent 55e7e30 commit be29cf0

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

src/guess/guess.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,24 @@ var primaryKeyTypes = map[string]bool{
2121

2222
type GuessOption func(database.Schema, database.PrimaryKey) bool
2323

24-
func isPrimaryKeyType(dataType string) bool {
25-
_, ok := primaryKeyTypes[dataType]
26-
return ok
24+
func isAcceptableAsPrimaryKey(columnType, primaryKeyType string) bool {
25+
_, colIsOk := primaryKeyTypes[columnType]
26+
_, pkIsOk := primaryKeyTypes[primaryKeyType]
27+
return colIsOk && pkIsOk && columnType == primaryKeyType
2728
}
2829

2930
// Recongnize a column thats same name of other table's primary key is a foreign key
3031
// This base idea refers to SchemaSpy DbAnalyzer:
3132
// https://github.com/schemaspy/schemaspy/blob/master/src/main/java/org/schemaspy/DbAnalyzer.java
3233
func GuessByPrimaryKey() GuessOption {
3334
return func(s database.Schema, pk database.PrimaryKey) bool {
34-
return isPrimaryKeyType(pk.DataType) && s.DataType == pk.DataType && s.Column == pk.Column && pk.Column != idColumn
35+
return isAcceptableAsPrimaryKey(s.DataType, pk.DataType) && s.Column == pk.Column && pk.Column != idColumn
3536
}
3637
}
3738

3839
func GuessByTableAndColumn() GuessOption {
3940
return func(s database.Schema, pk database.PrimaryKey) bool {
40-
if !isPrimaryKeyType(pk.DataType) && s.DataType != pk.DataType {
41+
if !isAcceptableAsPrimaryKey(s.DataType, pk.DataType) {
4142
return false
4243
}
4344

0 commit comments

Comments
 (0)