Skip to content

Commit 56e9b94

Browse files
authored
fix cockroachdb index name inspection (#122)
1 parent 3ffd6ab commit 56e9b94

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

migrator.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -436,22 +436,35 @@ func (m Migrator) ColumnTypes(value interface{}) (columnTypes []gorm.ColumnType,
436436

437437
// check primary, unique field
438438
{
439-
columnTypeRows, err := m.DB.Raw("SELECT c.column_name, constraint_type FROM information_schema.table_constraints tc JOIN information_schema.constraint_column_usage AS ccu USING (constraint_schema, constraint_name) JOIN information_schema.columns AS c ON c.table_schema = tc.constraint_schema AND tc.table_name = c.table_name AND ccu.column_name = c.column_name WHERE constraint_type IN ('PRIMARY KEY', 'UNIQUE') AND c.table_catalog = ? AND c.table_schema = ? AND c.table_name = ?", currentDatabase, currentSchema, table).Rows()
439+
columnTypeRows, err := m.DB.Raw("SELECT constraint_name FROM information_schema.table_constraints tc JOIN information_schema.constraint_column_usage AS ccu USING (constraint_schema, constraint_name) JOIN information_schema.columns AS c ON c.table_schema = tc.constraint_schema AND tc.table_name = c.table_name AND ccu.column_name = c.column_name WHERE constraint_type IN ('PRIMARY KEY', 'UNIQUE') AND c.table_catalog = ? AND c.table_schema = ? AND c.table_name = ? AND constraint_type = ?", currentDatabase, currentSchema, table, "UNIQUE").Rows()
440440
if err != nil {
441441
return err
442442
}
443+
uniqueContraints := map[string]int{}
444+
for columnTypeRows.Next() {
445+
var constraintName string
446+
columnTypeRows.Scan(&constraintName)
447+
uniqueContraints[constraintName]++
448+
}
449+
columnTypeRows.Close()
443450

451+
columnTypeRows, err = m.DB.Raw("SELECT c.column_name, constraint_name, constraint_type FROM information_schema.table_constraints tc JOIN information_schema.constraint_column_usage AS ccu USING (constraint_schema, constraint_name) JOIN information_schema.columns AS c ON c.table_schema = tc.constraint_schema AND tc.table_name = c.table_name AND ccu.column_name = c.column_name WHERE constraint_type IN ('PRIMARY KEY', 'UNIQUE') AND c.table_catalog = ? AND c.table_schema = ? AND c.table_name = ?", currentDatabase, currentSchema, table).Rows()
452+
if err != nil {
453+
return err
454+
}
444455
for columnTypeRows.Next() {
445-
var name, columnType string
446-
columnTypeRows.Scan(&name, &columnType)
456+
var name, constraintName, columnType string
457+
columnTypeRows.Scan(&name, &constraintName, &columnType)
447458
for _, c := range columnTypes {
448459
mc := c.(*migrator.ColumnType)
449460
if mc.NameValue.String == name {
450461
switch columnType {
451462
case "PRIMARY KEY":
452463
mc.PrimaryKeyValue = sql.NullBool{Bool: true, Valid: true}
453464
case "UNIQUE":
454-
mc.UniqueValue = sql.NullBool{Bool: true, Valid: true}
465+
if uniqueContraints[constraintName] == 1 {
466+
mc.UniqueValue = sql.NullBool{Bool: true, Valid: true}
467+
}
455468
}
456469
break
457470
}

0 commit comments

Comments
 (0)