JOIN using sqlx causes 'missing destination name' - go

I have this query that works fine on mysql and return the intended result, but sqlx has difficulty to handle it:
var jokes []model.Joke
err := shared.Dbmap.Select(&jokes, "SELECT * FROM joke INNER JOIN jokevote ON joke.id=jokevote.joke_id AND jokevote.vote=? AND joke.user_id=?", 1, userId)
if err != nil {
log.Println(err)
}
At runtime, I get no result but this error message in terminal:
missing destination name joke_id
I don't have this issue when querying a single table.
So I'm wondering what is wrong here and how to fix it?
UPDATE: Here are the structs:
type Joke struct {
ID int `db:"id" json:"id"`
UserID int `db:"user_id" json:"user_id"`
Title string `db:"title" json:"title"`
Content string `db:"content" json:"content"`
...
}
type JokeVote struct {
ID int `db:"id" json:"id"`
JokeID int `db:"joke_id" json:"joke_id"`
UserID int `db:"user_id" json:"user_id"`
Vote int `db:"vote" json:"vote"`
}

When you execute "SELECT * FROM joke INNER JOIN jokevote" you will get columns from both joke and jokevote tables.
Try to query "SELECT joke.* FROM joke INNER JOIN jokevote" to get only columns from joke table.

Related

"doesn't have relation" from go-pg

I am trying to pull data for CountyEntity that related with CityEntity:
type CityEntity struct {
tableName struct{} `pg:"city,alias:ci,discard_unknown_columns"`
Id string `pg:"id"`
Name string `pg:"name"`
}
type CountyEntity struct {
tableName struct{} `pg:"county,alias:co,discard_unknown_columns"`
Id int64 `pg:"id"`
Name string `pg:"name"`
DefaultTargetXDockId int64 `pg:"defaulttargetxdockid"`
MapsPreference string `pg:"mapspreference"`
EmptyDistrictAllowed bool `pg:"empty_district_allowed"`
CityId int64 `pg:"cityid"`
City *CityEntity `pg:"fk:cityid"`
}
My query is:
db, _ := repository.pgRepository.CreateDBConnection(.....)
var counties []model.CountyEntity
err := db.Model(&counties).
Join("inner join test.city ci on ci.id = co.cityid").
Relation("City").
Where("co.cityid = ?", cityId).
Select()
return counties, err
it throws that:
model=CountyEntity does not have relation="City"
But actually, I have the relation between city and county table on the database.
Db Relation Image
I tried different ways to solve but I couldn't solve it. Does anybody have an idea about what is the root cause of it and what is the possible solutions?
Pretty old and I'm sure you've figured it out by now, but for anyone else that stumbles across this your model should look like this
type CountyEntity struct {
tableName struct{} `pg:"county,alias:co,discard_unknown_columns"`
Id int64 `pg:"id"`
Name string `pg:"name"`
DefaultTargetXDockId int64 `pg:"defaulttargetxdockid"`
MapsPreference string `pg:"mapspreference"`
EmptyDistrictAllowed bool `pg:"empty_district_allowed"`
CityId int64 `pg:"cityid"`
City *CityEntity `pg:"rel:has-one,fk:cityid"`
}
Notice the "rel:has-one" added to the city column. You can find more information about all of these here: https://pg.uptrace.dev/models/

Gorm WHERE clause not working on Preload data

This query here below works, where the where clause works becuase it's using values from ResourceUsage struct, but I would love to be able to do something like this, where the where clause is using value from the ResourceMetadata struct.
db.Preload("ResourceMetadata").Where("resource_type = ?", resourceType).Where("timestamp BETWEEN ? AND ?", start, end).Limit(10).Find(&resourceUsage)
But it throws exception:
2019-12-03 11:06:12] Error 1054: Unknown column 'resource_type' in 'where clause'
Code:
// ResourceUsage describes the storage model for resource-usage
type ResourceUsage struct {
ID int64 `json:"-"`
DetailsID int64 `json:"details_id"`
ResourceMetadata *ResourceMetadata `gorm:"foreignkey:DetailsID;association_foreignkey:ID"`
MeasuredType string `json:"measured_type"`
Quantity float64 `json:"quantity"`
Timestamp int64 `json:"timestamp"`
}
// ResourceMetadata describes the storage model for resource-usage
type ResourceMetadata struct {
ID int64 `json:"-"`
ResourceUUID string `json:"resource_uuid"`
ResourceName string `json:"resource_name"`
ResourceDisplayName string `json:"resource_display_name"`
ResourceType string `json:"resource_type"`
}
db.Preload("ResourceMetadata").Where("timestamp BETWEEN ? AND ?", start, end).Limit(10).Find(&resourceUsage)
Can you use a JOIN?
resourceUsages := []ResourceUsage{}
if err := db.Joins("JOIN resource_metadata ON resource_metadata.id=details_id").
Where("resource_metadata.resource_type = ? AND timestamp BETWEEN ? AND ?", resourceType, start, end).
Preload("ResourceMetadata").Limit(10).
Find(&resourceUsages).Error; err != nil {
// ... handle error ...
}

How to convert sqlx query results to an array of structs?

I am trying to query all the results from a postgres table without where condition and map it with array of structs with the help of sqlx db Query by passing the args ...interface {}.
But the code pasted below never works, Instead of iterating and scanning the result one by one , is it possible to get the following code work ??
Inputs are much appreciated . Thank you
type CustomData struct {
ID string `db:"id" json:",omitempty"`
Name string `db:"name" json:",omitempty"`
Description string `db:"description" json:",omitempty"`
SourceID string `db:"sourceid" json:",omitempty"`
StatusID string `db:"statusid" json:",omitempty"`
StatusReason string `db:"statusreason" json:",omitempty"`
CreateTime string `db:"createtime" json:",omitempty"`
UpdateTime string `db:"updatetime" json:",omitempty"`
}
var myData []CustomData
*sqlx.DB.Query("SELECT id as ID, name as Name, description as Description, sourceid as SourceID, statusid as StatusID, statusreason as StatusReason, createtime as CreateTime, updatetime as UpdateTime FROM myschema.my_table", &myData)
// tried with following statement but din't work either
// *sqlx.DB.Query("SELECT * FROM myschema.my_table", &myData)
for _, data := range myData {
fmt.Println("--", data)
}
Expected results:
--- CustomData{1,x,x,x,x}
--- CustomData{2,x,x,x,x}
Actual:
Nothing..
You don't need to rename the fields in the query, since you're defining the actual DB fields in the struct tags.
If you want to scan directly to the slice of CustomData and if you are using SQLX, you should use the SQLX specific Select method, rather than the generic SQL Query. Slightly modified relevant example from the illustrated guide to SQLX (https://jmoiron.github.io/sqlx/#getAndSelect):
pp := []Place{}
err = db.Select(&pp, "SELECT * FROM place")
So in your case:
myData := []CustomData
err = db.Select(&myData, "SELECT * FROM myschema.my_table")
you can use the following:
for rows.Next() {
s := CustomData{}
if err := rows.Scan(&s); err != nil {
return err
}
fmt.Println(s)
}
and you can always use ORM library as gorm if you like code first approach or sqlboiler if you like DB first approach

Golang: sqlx StructScan mapping db column to struct

i have my model struct like the below :
type Detail struct {
Product
Stocks
}
type Product struct {
Name string `db:"name"`
Id int `db:"id"`
}
type Stocks {
Name string `db:"name"`
Price float `db:"price"`
Type string `db:"type"`
}
i would have a query to join the above tables like the below :
query, args, err := sqlx.In("select p.name , s.price from Product p,Stocks s where p.name=s.name and type IN (?)",typecodes)
query = s.Cmd.Db.Rebind(query)
var rows *sqlx.Rows
rows, err = s.Cmd.Db.Queryx(query, args...)
for rows.Next() {
var p model.Detail
err = rows.StructScan(&p)
}
Would like to know when i do rows.StructScan(&p) will the Product structure Name field be populated or will there be any ambuigity found for the same since Stocks also have a Name field ?
Currently i am not getting any result for the above.But when i comment the Name field in the Stocks struct, i am getting the data.
Let me know what i am missing here.
For ambiguous fields you're best annotating them with a prefix of their struct name, e.g. product_name, stock_name, then alias them appropriately in your SQL statement.
I.e.
type Detail struct {
Product
Stocks
}
type Product struct {
Name string `db:"product_name"`
Id int `db:"id"`
}
type Stocks {
Name string `db:"stock_name"`
Price float `db:"price"`
Type string `db:"type"`
}
And in your SQL:
SELECT p.name AS product_name, s.name AS stock_name, ... FROM Product p, Stocks s WHERE ...

How to load database records into sub structs?

Playing around with GO/Beego Framework and trying to query the database to load some records into a struct.
Bellow is the important code:
type User struct {
UserId int64 `orm:"pk"`
FirstName string `orm:"null" valid:"MinSize(2);MaxSize(150)"`
LastName string `orm:"null" valid:"MinSize(2);MaxSize(150)"`
Email string `valid:"Required;MinSize(2);MaxSize(150);Email" required:"true" description:"user email address"`
Password string `valid:"Required;MaxSize(60)" required:"true" description:"user plain text password" json:"-"`
AccessLevel uint64 `json:"-"`
AuthKey string `json:"-"`
Status int `json:"-"`
DateAdded time.Time `orm:"-" json:"-"`
LastUpdated time.Time `orm:"-" json:"-"`
// relations
Profile *UserProfile `orm:"rel(one)" json:"-"` // OneToOne relation
}
type UserProfile struct {
UserId int64 `orm:"pk"` // doesn't work without a PK, doesn't make sense, it's a fk...
Company string `orm:"null" valid:"MinSize(2);MaxSize(150)"`
VatNumber string `orm:"null" valid:"MinSize(2);MaxSize(150)"`
Website string `orm:"null" valid:"MinSize(2);MaxSize(150)"`
Phone string `orm:"null" valid:"MinSize(2);MaxSize(150);Mobile"`
Address1 string `orm:"null" valid:"MinSize(2);MaxSize(255)"`
Address2 string `orm:"null" valid:"MinSize(2);MaxSize(255)"`
City string `orm:"null" valid:"MinSize(2);MaxSize(150)"`
State string `orm:"null" valid:"MinSize(2);MaxSize(150)"`
Zip string `orm:"null" valid:"MinSize(4);MaxSize(15)"`
Country string `orm:"null" valid:"MinSize(2);MaxSize(150)"`
ConfirmationKey string `orm:"null" valid:"Length(40)" json:"-"`
// relations
User *User `orm:"reverse(one)" json:"-"` // Reverse relationship (optional)
}
func GetAllUsers() []User {
o := orm.NewOrm()
var users []User
sql := `
SELECT user.*, user_profile.*
FROM user
INNER JOIN user_profile ON user_profile.user_id = user.user_id
WHERE 1`
_, err := o.Raw(sql).QueryRows(&users)
if err != nil {
beego.Error(err)
}
beego.Info(users)
return users
}
Now the problem with above is that upon calling GetAllUsers() the embed struct from User, that is Profile, doesn't get populated, so how would i go so that embed structs are also populated?
I have also tried to get the users with:
o.QueryTable("user").Filter("status", STATUS_ACTIVE).RelatedSel("Profile").All(&users)
which produces a sql query like:
SELECT T0.`user_id`, T0.`first_name`, T0.`last_name`, T0.`email`, T0.`password`, T0.`access_level`, T0.`auth_key`, T0.`status`, T0.`profile_id`, T1.`user_id`, T1.`company`, T1.`vat_number`, T1.`website`, T1.`phone`, T1.`address1`, T1.`address2`, T1.`city`, T1.`state`, T1.`zip`, T1.`country`, T1.`confirmation_key` FROM `user` T0 INNER JOIN `user_profile` T1 ON T1.`user_id` = T0.`profile_id` WHERE T0.`status` = ? LIMIT 1000
And i don't know from where it came up with the join on profile_id column since that doesn't even exists and i am not sure how to specify the right column to join, seems it takes it from the structure name. Also, i don't like the fact that there isn't a way to specify what to select.
Any hint is highly appreciated, i am sure it's something simple that i miss.
Thanks.
https://github.com/astaxie/beego/issues/384 has a relative talk, but it's Chinese.The key is below:
type User struct {
Id int
Name string
}
type Profile struct {
Id int
Age int
}
var users []*User
var profiles []*Profile
err := o.Raw(`SELECT id, name, p.id, p.age FROM user
LEFT OUTER JOIN profile AS p ON p.id = profile_id WHERE id = ?`, 1).QueryRows(&users, &profiles)

Resources