Does it support on two scene in postgres? So if have column xxx, i get xxx.if not, i get the default value(string is "", int is zero and so on).Because I'm not sure if the column exists(such as code upgrade but database not). #1981

Closed
opened 2021-07-08 10:02:53 +00:00 by wangchanggan · 5 comments

For example:

type User struct {
Name string json:"name"
Age int json:"age,omitempty"
}

i get data from postgres and database User have column name but not age, but i can set tag to ignore it, so i still get user data.

thanks!

For example: type User struct { Name string `json:"name"` Age int `json:"age,omitempty"` } i get data from postgres and database User have column name but not age, but i can set tag to ignore it, so i still get user data. thanks!
Owner

Use - just like json tag.

type User struct {
  Name string `json:"name"`
  Age int `xorm:"-" json:"age,omitempty"`
}
Use `-` just like `json` tag. ```go type User struct { Name string `json:"name"` Age int `xorm:"-" json:"age,omitempty"` } ```
lunny added the
need
feedback
label 2021-07-08 14:56:03 +00:00
Author

but if database User have column Age, it will not return.
Does it support on two scene? if have column Age, i get name and age.if not, i get name only.

but if database User have column Age, it will not return. Does it support on two scene? if have column Age, i get name and age.if not, i get name only.
Owner

It has been supported. If results have no column age, it will not be assigned a value. it will be zero.

It has been supported. If results have no column `age`, it will not be assigned a value. it will be zero.
Author

It has been supported. If results have no column age, it will not be assigned a value. it will be zero.

yes,because the default value of int type is zero in golang.but if use

Name string xorm:"-"

if have column Name, i can't get name.

> It has been supported. If results have no column `age`, it will not be assigned a value. it will be zero. yes,because the default value of int type is zero in golang.but if use Name string `xorm:"-"` if have column Name, i can't get name.
wangchanggan changed title from Does it support ignore param when column does not exist in postgres? to Does it support on two scene in postgres? if have column xxx, i get xxx.if not, i get the default value(string is "", int is zero and so on). 2021-07-13 14:29:12 +00:00
wangchanggan changed title from Does it support on two scene in postgres? if have column xxx, i get xxx.if not, i get the default value(string is "", int is zero and so on). to Does it support on two scene in postgres? So if have column xxx, i get xxx.if not, i get the default value(string is "", int is zero and so on).Because I'm not sure if the column exists(such as code upgrade but database not). 2021-07-13 14:33:45 +00:00
Owner

When you use Get, the first return parameter will tell you if the record exist. And you can also create the struct with default fields values. So that if the record is not exist on database, the value will not be changed.

I will not support that in xorm.

When you use `Get`, the first return parameter will tell you if the record exist. And you can also create the struct with default fields values. So that if the record is not exist on database, the value will not be changed. I will not support that in xorm.
lunny added the
wontfix
label 2021-07-13 15:02:11 +00:00
Sign in to join this conversation.
No Milestone
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: xorm/xorm#1981
No description provided.