diff --git a/dialects/postgres.go b/dialects/postgres.go index 96ebfc85..b46fb711 100644 --- a/dialects/postgres.go +++ b/dialects/postgres.go @@ -966,38 +966,35 @@ func (db *postgres) AutoIncrStr() string { } func (db *postgres) CreateTableSQL(table *schemas.Table, tableName string) ([]string, bool) { - var sql string - sql = "CREATE TABLE IF NOT EXISTS " if tableName == "" { tableName = table.Name } quoter := db.Quoter() - sql += quoter.Quote(tableName) - sql += " (" + var b strings.Builder + b.WriteString("CREATE TABLE IF NOT EXISTS ") + quoter.QuoteTo(&b, tableName) + b.WriteString(" (") - if len(table.ColumnsSeq()) > 0 { - pkList := table.PrimaryKeys + for i, colName := range table.ColumnsSeq() { + col := table.GetColumn(colName) + s, _ := ColumnString(db, col, col.IsPrimaryKey && len(table.PrimaryKeys) == 1) + b.WriteString(s) - for _, colName := range table.ColumnsSeq() { - col := table.GetColumn(colName) - s, _ := ColumnString(db, col, col.IsPrimaryKey && len(pkList) == 1) - sql += s - sql = strings.TrimSpace(sql) - sql += ", " + if len(table.PrimaryKeys) > 1 { + b.WriteString("PRIMARY KEY ( ") + b.WriteString(quoter.Join(table.PrimaryKeys, ",")) + b.WriteString(" )") } - if len(pkList) > 1 { - sql += "PRIMARY KEY ( " - sql += quoter.Join(pkList, ",") - sql += " ), " + if i != len(table.ColumnsSeq())-1 { + b.WriteString(", ") } - - sql = sql[:len(sql)-2] } - sql += ")" - return []string{sql}, true + b.WriteString(")") + + return []string{b.String()}, false } func (db *postgres) IndexCheckSQL(tableName, idxName string) (string, []interface{}) {