SQLX“缺少目标名称"在struct标记中使用表名时 [英] SQLX "missing destination name" when using table name in the struct tag

查看:125
本文介绍了SQLX“缺少目标名称"在struct标记中使用表名时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题在于,当我将struct标记与对象一起使用时,它们无法正常工作.在此之前,我曾从事过一些项目,虽然做过同样的事情,但没有任何问题,但我不知道为什么.

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.

示例:

这不起作用:

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

收到错误: * []类别

这很好:

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

查询:

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

在SQL编辑器中运行查询就可以了.我还参与了一个专有项目,在该项目中,将表名放在标签之前可以很好地工作,但是出于任何原因,我似乎都无法将其与之配合使用.

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.

感谢帮助

使用mysql

推荐答案

该错误实际上是在连接步骤中造成的!

The mistake was actually made in the connection step!

我需要将 columnsWithAlias = true 添加到连接参数,并且代码工作正常.

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

感谢RayfenWindspear提示mysql默认情况下不发送列名.

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

这篇关于SQLX“缺少目标名称"在struct标记中使用表名时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆