使用'和'指示数据库列名称的正确方法是什么?
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
}
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