Please help me to explain why this code fail, why on json result trip_from has empty string :
type Trip struct {
ID int `json:"id" gorm:"primary_key"`
Name string `json:"name" gorm:"column:name"`
Estimate int `json:"estimate" gorm:"column:estimate"`
Departure time.Time `json:"departure" gorm:"column:departure"`
Price int `json:"price" gorm:"column:price"`
Slot int `json:"slot" gorm:"column:slot"`
FromId int `json:"from_id" gorm:"column:from_id"`
TripFrom string `json:"trip_from" gorm:"column:trip_from"`
ToId int `json:"to_id" gorm:"column:to_id"`
To string `json:"trip_to" gorm:"-"`
Thumbnail string `json:"thumbnail" gorm:"column:thumbnail"`
Slug string `json:"slug" gorm:"column:slug"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
var trips []model.Trip
err := e.DB.Debug().
Select("trips.*, cities.name as trip_from").
Joins("JOIN cities ON cities.id = trips.from_id").
Where("trips.id = ?", "1").
Find(&trips)
if err.Error != nil {
helper.CommonLogger().Error(err)
fmt.Printf("[tripRepository.ReadAll] error execute query %v n", err)
return nil, fmt.Errorf("failed view all data")
}
fmt.Println(trips)
return &trips, nil
Why json result trip_from return empty string
4
May be you can try this,Adjust the value “1” in the Where condition from “trips.id = ?” to the integer 1.
New contributor
catalpa is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.