SQLX “missing destination name” when using table name in the struct tag

本小妞迷上赌 提交于 2021-01-29 19:35:27

问题


The issue is that when I use struct tags with an object, they do not work properly. I've worked on projects before that have done the same thing but have had no issue, but I can't figure out why.

Example:

this does not work:

type Category struct {  
   ID          int            `json:"id" db:"category.id"`  
   Name        string         `json:"name" db:"category.name"`   
   Description string         `json:"description" db:"category.description"` 
}

error received: missing destination name id in *[]Category

this works fine:

type Category struct {  
   ID          int            `json:"id" db:"id"`   
   Name        string         `json:"name" db:"name"`    
   Description string         `json:"description" db:"description"` 
}

query:

result := []Category{}
query := `
    SELECT category.id, category.name, category.description FROM category;
    `
err := sqlx.Select(db, &result, query)

Running the query in a SQL editor works just fine. I also have worked on a proprietary project where prepending the table name to the tag works fine but for whatever reason I can't seem to get it going with this.

Appreciate the help,

EDIT:

using mysql


回答1:


The mistake was actually made in the connection step!

I needed to add columnsWithAlias=true to the connection parameters and the code worked fine.

Thanks to RayfenWindspear for the tip that mysql doesn't send column names by default.



来源:https://stackoverflow.com/questions/53655515/sqlx-missing-destination-name-when-using-table-name-in-the-struct-tag

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!