When querying data, an alias is set for a field. How do I use GORM to read the value of the alias field into the structure?
Table Structure
DROP TABLE IF EXISTS "test"."test";
CREATE TABLE "test"."test" (
"id" varchar(32) NOT NULL,
"name" varchar(255) COLLATE "pg_catalog"."default",
"remark" varchar(255) COLLATE "pg_catalog"."default"
);
ALTER TABLE "test"."test" ADD CONSTRAINT "test_pkey" PRIMARY KEY ("id");
Table corresponding model structure
type Test struct {
ID string `gorm:"column:id;type:varchar(32);primaryKey;comment:Unique ID" json:"id"`
Name string `gorm:"column:name;type:varchar(255);comment:Name Info" json:"name"`
Remark string `gorm:"column:remark;type:varchar(255);comment:Remark Info" json:"remark"`
MoreInfo string `gorm:"-" json:"moreInfo"` // Non-table field
}
GORM query
gormDB
is the initialized gorm database connection.
test := Test{ID: "0000000001"}
gormDB.Select("*, 'testMoreInfoVal' AS more_Info").Where(&test).Find(&test)
Q: How to use GORM to read the alias field more_Info
that does not exist in the table into the MoreInfo
property of the Test
structure?