Append column comments to create table sentence when using postgres
This commit is contained in:
parent
69c17fafbb
commit
d150d85bfd
@ -432,8 +432,9 @@ func (mc *_modelCache) getDbCreateSQL(al *alias) (queries []string, tableIndexes
|
|||||||
columns := make([]string, 0, len(mi.fields.fieldsDB))
|
columns := make([]string, 0, len(mi.fields.fieldsDB))
|
||||||
|
|
||||||
sqlIndexes := [][]string{}
|
sqlIndexes := [][]string{}
|
||||||
|
var commentIndexes []int // store comment indexes for postgres
|
||||||
|
|
||||||
for _, fi := range mi.fields.fieldsDB {
|
for i, fi := range mi.fields.fieldsDB {
|
||||||
|
|
||||||
column := fmt.Sprintf(" %s%s%s ", Q, fi.column, Q)
|
column := fmt.Sprintf(" %s%s%s ", Q, fi.column, Q)
|
||||||
col := getColumnTyp(al, fi)
|
col := getColumnTyp(al, fi)
|
||||||
@ -475,8 +476,12 @@ func (mc *_modelCache) getDbCreateSQL(al *alias) (queries []string, tableIndexes
|
|||||||
}
|
}
|
||||||
|
|
||||||
if fi.description != "" && al.Driver != DRSqlite {
|
if fi.description != "" && al.Driver != DRSqlite {
|
||||||
|
if al.Driver == DRPostgres {
|
||||||
|
commentIndexes = append(commentIndexes, i)
|
||||||
|
} else {
|
||||||
column += " " + fmt.Sprintf("COMMENT '%s'", fi.description)
|
column += " " + fmt.Sprintf("COMMENT '%s'", fi.description)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
columns = append(columns, column)
|
columns = append(columns, column)
|
||||||
}
|
}
|
||||||
@ -515,6 +520,19 @@ func (mc *_modelCache) getDbCreateSQL(al *alias) (queries []string, tableIndexes
|
|||||||
}
|
}
|
||||||
|
|
||||||
sql += ";"
|
sql += ";"
|
||||||
|
if al.Driver == DRPostgres && len(commentIndexes) > 0 {
|
||||||
|
// append comments for postgres only
|
||||||
|
for _, index := range commentIndexes {
|
||||||
|
sql += fmt.Sprintf("\nCOMMENT ON COLUMN %s%s%s.%s%s%s is '%s';",
|
||||||
|
Q,
|
||||||
|
mi.table,
|
||||||
|
Q,
|
||||||
|
Q,
|
||||||
|
mi.fields.fieldsDB[index].column,
|
||||||
|
Q,
|
||||||
|
mi.fields.fieldsDB[index].description)
|
||||||
|
}
|
||||||
|
}
|
||||||
queries = append(queries, sql)
|
queries = append(queries, sql)
|
||||||
|
|
||||||
if mi.model != nil {
|
if mi.model != nil {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user