Your issue may already be reported! Please search on the issue track before creating one.
go version)?go1.12
mysql 5.7.19-log
mysql> SELECT VERSION();
+------------+
| VERSION() |
+------------+
| 5.7.19-log |
+------------+
1 row in set (0,00 sec)
Need to runnable with GORM's docker compose config or please provides your config.
package main
import (
"fmt"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql"
"os"
)
type User struct {
gorm.Model
ID string `gorm:"column:id;type:varchar(36);unique;not null;primary_key"`
CognitoID string `gorm:"column:cognito_id;type:varchar(255);unique;not null"`
Email string `gorm:"column:email;type:varchar(255);unique;not null"`
DisplayName string `gorm:"column:display_name;type:varchar(255);not null"`
LastName string `gorm:"column:last_name;type:varchar(255);not null"`
FirstName string `gorm:"column:first_name;type:varchar(255);not null"`
Phone string `gorm:"column:phone;type:varchar(255)"`
Avatar string `gorm:"column:avatar;type:varchar(255)"`
}
var db *gorm.DB
func getDBConnection() (connection *gorm.DB, err error) {
user := os.Getenv("MYSQL_USER")
password := os.Getenv("MYSQL_PASSWORD")
host := os.Getenv("MYSQL_HOST")
database := "alquilando"
connectionString := fmt.Sprintf(
"%s:%s@tcp(%s)/%s?charset=utf8&parseTime=True&loc=Local",
user, password, host, database,
)
connection, err = gorm.Open("mysql", connectionString)
return connection, err
}
func GetUsers() (users *[]User, err error) {
var db *gorm.DB
db, err = getDBConnection()
if err != nil {
fmt.Println("GetUsers(): got the following error: ", err)
}
defer db.Close()
var usersArray []User
db.Find(&usersArray)
return &usersArray, err
}
func init() {
gorm.DefaultTableNameHandler = func(db *gorm.DB, defaultTableName string) string {
return "al_" + defaultTableName[:len(defaultTableName)-1]
}
}
func main() {
var usersArray []models.User
users, err := GetUsers()
}
As u see In my code, I'm attempting to override the ID field (type=string), but (when calling GetUsers() ) db.Find(&usersArray) throws the following error:
[2019-06-20 19:34:02] sql: Scan error on column index 0, name "id": converting driver.Value type []uint8 ("d298b1ca-93a3-11e9-af65-9265421abe6c") to a uint: invalid syntax.
Very likely i'm doing something wrong, do u guys have any insights on this?
Thank you!
I finally realized that I was trying to use composition in the wrong way, this one can be closed.
Any elaboration on what went wrong here? I'm having the same issue and can't wrap my head around it...
Most helpful comment
Any elaboration on what went wrong here? I'm having the same issue and can't wrap my head around it...