Hi, I have this structure in go:
type Transaction struct {
Id int64
ReaderId int64
Amount int
}
SQLite3 schema:
CREATE TABLE tr(Id INTEGER PRIMARY KEY AUTOINCREMENT, ReaderId INTEGER(8), Amount INTEGER);
When I'm trying to load this structure from DB
t := &Transaction{}
err = DB.Get(t, "SELECT * FROM tr WHERE Id=$1", id)
I always get an error: "missing destination name Id"
Just looked at the sqlx code and realized that I have to use different mapper bc column names in my table are equal to structure's fields names:
sqlx.NameMapper = func(s string) string { return s }
Not very elegant still but works.
I don't understand this problem still. I am having the same issue, but wouldn't you want this to work? Naming your struct fields the same as your database columns?
@danriipl use inline tags:
type Transaction struct {
Id int64 `db:"Id"`
ReaderId int64 `db:"ReaderId"`
Amount int `db:"Amount"`
}
You need to use mapper only if you need to change default behaviour like converting struct's field names to snake_case or camelCase or whatever you need. But I would suggest to stick with inline tags.
How to map multiple rows into a struct.
Like this:
type Transaction struct {
Id int64 `db:"Id"`
ReaderId int64 `db:"ReaderId"`
Amount int `db:"Amount"`
}
AND then I have
type Transactions {
trans []Transaction
}
And I would like to select the data and the query returns multiple rows. How to map them into a Transactions struct?
Most helpful comment
I don't understand this problem still. I am having the same issue, but wouldn't you want this to work? Naming your struct fields the same as your database columns?