dtdvbf37193 2018-12-06 16:16
浏览 204
已采纳

在struct标记中使用表名称时,SQLX“缺少目标名称”

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条回答 默认 最新

  • dongluo6343 2018-12-07 20:29
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度