Hello, I'm trying to make SELECT query to table with column name that contain number:
Select("id AS IDENT, t245a AS TBB1A").Table("items")
and scan result to this struct:
resultData := []struct {IDENT int TBB1A string}{}
this what I get:
[{1 } {2 }]
But when I remove number from the column name, like this:
Select("id AS IDENT, t245a AS TBBBA").Table("items")
resultData := []struct {IDENT int TBBBA string}{}
it work fine:
[{1 My First item} {2 My Secont item}]
How can I make queries to column names like this - TBB1A?
PostgreSQL 14.11 (Ubuntu 14.11-0ubuntu0.22.04.1) on x86_64-pc-linux-gnu
Comment From: github-actions[bot]
The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking ✨
Comment From: ivila
@rubymongoose Because you didn't set the column name manually, so gorm use NamingStrategy to name your field, check this: https://github.com/go-gorm/gorm/blob/956f7ce84309ccd5631b28335c814f4063eac58e/schema/naming.go#L129 so: 1) struct {IDENT int TBB1A string}: Field TBB1A auto named to tbb1_a 2) struct {IDENT int TBBBA string}: Field TBBAA auto named to tbbaa
that's why. Setting the column name manually can solve your problem.
struct {
IDENT int
TBBBA string `gorm:"column:tbb1a"` // you should set this
}
Comment From: github-actions[bot]
The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking ✨
Comment From: rubymongoose
@rubymongoose Because you didn't set the column name manually, so gorm use NamingStrategy to name your field, check this:
https://github.com/go-gorm/gorm/blob/956f7ce84309ccd5631b28335c814f4063eac58e/schema/naming.go#L129
so:
1. struct {IDENT int TBB1A string}: Field TBB1A auto named to tbb1_a 2. struct {IDENT int TBBBA string}: Field TBBAA auto named to tbbaathat's why. Setting the column name manually can solve your problem.
go struct { IDENT int TBBBA string `gorm:"column:tbb1a"` // you should set this }
Thanks for the help, it works :)