英文:
What is the correct way to use ' and " for indicating db column names?
问题
我在我的数据库中有一张表,其中所有列的列名都以大写字母开头(例如:FMR_Number,Primary_Value)。列名是我无法控制的。数据库中的所有其他列(在其他表中)都是小写字母。我已经将一个结构体映射到了这个表,但是在查询时我一直收到一个缺少目标的错误。我相信这是因为名称映射器被设置为 ToLower(这是其他地方所需的)。我尝试使用以下格式来显式指定映射:
StructField string 'db:"RealColumnName"'
但是我得到了一个非法的 rune 字面量错误。当我查看其他人的代码示例时,我看到它以我刚刚尝试的方式表示。如果我将 ' 和 " 颠倒为:
StructField string "db:'RealColumnName'"
我就不会得到非法的 rune 字面量错误,但是我会得到缺少目标的错误。我在这里做错了什么?我正在使用 sqlx。
我的结构体:
type pah struct {
FMR_Fund_Number string 'db:"FMR_Fund_Number"'
Business_Data_Date string 'db:"Business_Data_Date"'
Outbound_Composition_Code string 'db:"Outbound_Composition_Code"'
Composition_Sub_Code string 'db:"Composition_Sub_Code"'
Composition_Sub_Code_Description string 'db:"Composition_Sub_Code_Description"'
Position_Name string 'db:"Position_Name"'
Primary_Value float64 'db:"Primary_Value"'
Record_Order int 'db:"Record_Order"'
Tier_Indicator string 'db:"Tier_Indicator"'
}
我的数据库调用:
func loadAssetAllocation() AssetAllocations {
pahGroup := []pah{}
pahQuery := "SELECT FMR_Fund_Number,Business_Data_Date,Outbound_Composition_Code,Composition_Sub_Code,Composition_Sub_Code_Description,Primary_Value,Tier_Indicator,Record_Order FROM PAH WHERE FMR_Fund_Number = \"312\" AND Outbound_Composition_Code = \"MDCAT\" AND Business_Data_Date = (SELECT Business_Data_Date FROM PAH ORDER BY Business_Data_Date DESC LIMIT 1) AND Composition_Sub_Code <> \"TAXADV\" ORDER BY Record_Order ASC"
assetAlloc := AssetAllocations{}
err := ffDB.Select(&pahGroup, pahQuery)
fmt.Println("pahQuery AssetAllocation error: ", err)
assetAlloc.AssetAll = pahGroup
return assetAlloc
}
英文:
I have a table in my DB that the column names all start with caps (ie: FMR_Number, Primary_Value). The column name are out of my control. All the other columns in the DB (in other tables) are lower case. I have mapped a struct to the table but I keep getting a missing destination err from the query. I believe it is because the name mapper is set to ToLower (which is what it needs for everywhere else). I attempted to use this format
StructField string 'db:"RealColumnName"'
to specify the mapping explicitly but I get an illegal rune literal. When I go look at examples in other peoples code I see it being represented in the way I just tried it. If I reverse the ' and " to
StructField string "db:'RealColumnName'"
I don't get the illegal rune literal error but I do get the missing destination err. What am I doing wrong here? I am using sqlx.
My Struct
type pah struct {
FMR_Fund_Number string 'db:"FMR_Fund_Number"'
Business_Data_Date string 'db:"Business_Data_Date"'
Outbound_Composition_Code string 'db:"Outbound_Composition_Code"'
Composition_Sub_Code string 'db:"Composition_Sub_Code"'
Composition_Sub_Code_Description string 'db:"Composition_Sub_Code_Description"'
Position_Name string 'db:"Position_Name"'
Primary_Value float64 'db:"Primary_Value"'
Record_Order int 'db:"Record_Order"'
Tier_Indicator string 'db:"Tier_Indicator"'
}
My db call
func loadAssetAllocation() AssetAllocations {
pahGroup := []pah{}
pahQuery := "SELECT FMR_Fund_Number,Business_Data_Date,Outbound_Composition_Code,Composition_Sub_Code,Composition_Sub_Code_Description,Primary_Value,Tier_Indicator,Record_Order FROM PAH WHERE FMR_Fund_Number = \"312\" AND Outbound_Composition_Code = \"MDCAT\" AND Business_Data_Date = (SELECT Business_Data_Date FROM PAH ORDER BY Business_Data_Date DESC LIMIT 1) AND Composition_Sub_Code <> \"TAXADV\" ORDER BY Record_Order ASC"
assetAlloc := AssetAllocations{}
err := ffDB.Select(&pahGroup, pahQuery)
fmt.Println("pahQuery AssetAllocation error: ", err)
assetAlloc.AssetAll = pahGroup
return assetAlloc
}
答案1
得分: 2
结构标签使用反引号(`)字符(通常在美国键盘布局的Esc键下方),而不是单引号。
你的结构体应该像下面这样,注意在变量名中使用下划线在Go语言中不是特别惯用的。
使用结构标签的原因通常是将Go导出的(首字母大写的)结构体字段名称映射到其他内容使用的名称(JSON、数据库、HTML表单等)。
type pah struct {
FMRFundNumber string `db:"FMR_Fund_Number"`
BusinessDataDate string `db:"Business_Data_Date"`
OutboundCompositionCode string `db:"Outbound_Composition_Code"`
CompositionSubCode string `db:"Composition_Sub_Code"`
CompositionSubCodeDescription string `db:"Composition_Sub_Code_Description"`
PositionName string `db:"Position_Name"`
PrimaryValue float64 `db:"Primary_Value"`
RecordOrder int `db:"Record_Order"`
TierIndicator string `db:"Tier_Indicator"`
}
参考文档:http://golang.org/pkg/reflect/#StructTag 和 http://golang.org/pkg/encoding/json/#Marshal
英文:
Struct tags use backticks - the ` character (commonly under the Esc key on US keyboard layouts) - not single quotes.
Your struct should look like the below, noting that underscores in variable names aren't particularly idiomatic in Go.
The reason you use struct tags is often to map exported (capitalised) Go struct field names to the names used by other things (JSON, databases, HTML forms, etc).
type pah struct {
FMRFundNumber string `db:"FMR_Fund_Number"`
BusinessDataDate string `db:"Business_Data_Date"`
OutboundCompositionCode string `db:"Outbound_Composition_Code"`
CompositionSubCode string `db:"Composition_Sub_Code"`
CompositionSubCodeDescription string `db:"Composition_Sub_Code_Description"`
PositionName string `db:"Position_Name"`
PrimaryValue float64 `db:"Primary_Value"`
RecordOrder int `db:"Record_Order"`
TierIndicator string `db:"Tier_Indicator"`
}
See the docs: http://golang.org/pkg/reflect/#StructTag and http://golang.org/pkg/encoding/json/#Marshal
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论