I have struct like this

type Menu struct {
    Name        string    `json:"name" gorm:"type:varchar(150);"`
    WebsiteName string    `json:"websiteName" gorm:"-"`
}

I want the WebsiteName to be able to scan but I do not want this field to be created in the Database. But when I add - the field does not get created in DB that's good but when I Scan(&menu) the field is not getting scanned.

How do I make this work ?

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: a631807682

Use permission tag.

WebsiteName  string `gorm:"<-:false;->:true"`

https://gorm.io/docs/models.html#Fields-Tags

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: khanakia

WebsiteName string `gorm:"<-:false;->:true"`

Hi, it still creates a Column in the Database table website_name when I run gorm AutoMigrate command. It should not create a column in the database

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: a631807682

I'm not sure what you mean, website_name is not a column in db, but should be able to scan? If website_name not a cloumn, what value needs to be scan into struct? Can you create a pull request to https://github.com/go-gorm/playground to describe it?

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: khanakia

@a631807682 Please check I have provided the example code. Basically, the website name will come from a different table.


type Menu struct {
    Name        string    `json:"name" gorm:"type:varchar(150);"`
    WebsiteName string    `json:"websiteName" gorm:"-"`
}

var records []Menu
query := db.Table("menus as m").Select("m.*, w.name as website_name")
query.Joins("INNER JOIN websites w on w.id=m.website_id").Find(&records)

## This returns
[
    {
        "name": "Menu1",
        "websiteName": ""
    },

    {
        "name": "Menu2",
        "websiteName": ""
    }
]

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: a631807682

@khanakia I haven't tried, but I think it might help you

WebsiteName  string `gorm:"<-:false;->;-:migration"`

<-:false no write permission, -> read permission, and -:migration ignore migration. https://github.com/go-gorm/gorm/pull/4028

Comment From: 260721735

i think you can use other struct for join select just like this

type Menu struct {
    Name        string    `json:"name" gorm:"type:varchar(150);"`
}
type JoinMenu struct {
    Menu        
    WebsiteName string    `json:"websiteName"`
}
var records []JoinMenu
query := db.Table("menus as m").Select("m.*, w.name as website_name")
query.Joins("INNER JOIN websites w on w.id=m.website_id").Find(&records)

model Menu use for insert,update,delete JoinMenu for complex queries with this design, database metadata model is safe when adding other complex queries

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: Fakerr

@khanakia I haven't tried, but I think it might help you

WebsiteName string `gorm:"<-:false;->;-:migration"`

<-:false no write permission, -> read permission, and -:migration ignore migration. #4028

@a631807682 The problem with this solution is that, if later I want to query my model without the joint table, it throws an error:

type Menu struct {
    Name        string    `json:"name" gorm:"type:varchar(150);"`
    WebsiteName string    `json:"websiteName" gorm:"<-:false;->;-:migration"`
}

var user Menu
db.Where("id = ?", userID).First(&user)

returns:
`ERROR: column menu.website_name does not exist`

Comment From: a631807682

@Fakerr ~~I will fix it later~~ You define it to have read permissions, but you expect it to sometimes and sometimes not, so you should use Omit

Comment From: Fakerr

@a631807682 Ideally I would like to set the permission to not read and write gorm:"-", however sometimes I would like to add a field in my model that don't exist in the database but that I can scan a value into it. I created a separate ticket describing what I want to https://github.com/go-gorm/gorm/issues/5284

Comment From: mtt0

@khanakia I haven't tried, but I think it might help you

WebsiteName string `gorm:"<-:false;->;-:migration"`

<-:false no write permission, -> read permission, and -:migration ignore migration. #4028

It'w worked for me with gorm v1.23.5, thank you very much~

Comment From: khanakia

@mtt0 Yes I fixed it precisely like that already.