type School struct {
ID int64 `gorm:"primary_key" json:"id"`
Name string `json:"name"`
}
type Student struct {
ID int64 `gorm:"primary_key" json:"id"`
Name string `json:"name"`
SchoolId int64 `json:"schoolId"`
}
// if column duplicat,rows.Columns() can add suffix such as name,name(1),name(2)?
func TestGORM(t *testing.T) {
result := []map[string]interface{}{}
tx := DB.Model(&Student{}).Select("students.*,b.*").Joins("left join schools as b on students.school_id = b.id")
rows, err := tx.Rows()
if err != nil {
panic(err)
}
cl, err := rows.Columns()
fmt.Println("columns:", cl)
tx = tx.Find(&result)
fmt.Println("result:", result)
err = tx.Error
if err != nil {
t.Errorf("Failed, got error: %v", err)
}
}
sql as this
SELECT students.*,b.* FROM "students" left join schools as b on students.school_id = b.id
but this sql can run right in postgresql.
https://github.com/sumumont/playground
if column duplicat,rows.Columns() can add suffix such as name,name(1),name(2)?
Comment From: github-actions[bot]
The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking ✨
Comment From: github-actions[bot]
The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking ✨
Comment From: black-06
Try Joins-Preloading:
type School struct {
ID int64 `gorm:"primary_key" json:"id"`
Name string `json:"name"`
}
type Student struct {
ID int64 `gorm:"primary_key" json:"id"`
Name string `json:"name"`
SchoolId int64 `json:"schoolId"`
}
func TestGORM(t *testing.T) {
type StudentResult struct {
Student
School School
}
result := StudentResult{}
db.Table("students").Joins("School").Find(&result)
}
SQL is
SELECT `students`.`id`,`students`.`name`,`students`.`school_id`,`School`.`id` AS `School__id`,`School`.`name` AS `School__name` FROM `students` LEFT JOIN `schools` `School` ON `students`.`school_id` = `School`.`id`
Comment From: github-actions[bot]
The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking ✨
Comment From: sumumont
I am making app like navicat , so I need get all cloumns.
Comment From: github-actions[bot]
The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking ✨
Comment From: black-06
if column duplicat, rows.Columns() can add suffix such as name,name(1),name(2)?
rows.Columns() is part of the golang standard library.
Or you can Scan yourself without GROM.
columnTypes, err := rows.ColumnTypes()
assert.Nil(t, err)
for _, columnType := range columnTypes {
fmt.Printf("%v\t", columnType.Name())
}
fmt.Println()
for rows.Next() {
values := make([]interface{}, len(columnTypes))
for idx, columnType := range columnTypes {
if columnType.ScanType() != nil {
values[idx] = reflect.New(reflect.PtrTo(columnType.ScanType())).Interface()
} else {
values[idx] = new(interface{})
}
}
err = rows.Scan(values...)
assert.Nil(t, err)
for _, value := range values {
if reflectValue := reflect.Indirect(reflect.Indirect(reflect.ValueOf(value))); reflectValue.IsValid() {
value = reflectValue.Interface()
if valuer, ok := value.(driver.Valuer); ok {
value, _ = valuer.Value()
} else if b, ok := value.(sql.RawBytes); ok {
value = string(b)
}
} else {
value = nil
}
fmt.Printf("%v\t", value)
}
fmt.Println()
}
std: | id | name | school_id | id | name | | -- | -- | -- | -- | -- | | 1 | student_1 | 1 | 1 | school_1 |
Comment From: github-actions[bot]
The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking ✨
Comment From: sumumont
tks.