Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

what is the diff of gorm tag - and -:all #7059

Open
zrss opened this issue Jun 10, 2024 · 0 comments
Open

what is the diff of gorm tag - and -:all #7059

zrss opened this issue Jun 10, 2024 · 0 comments
Assignees
Labels
type:question general questions

Comments

@zrss
Copy link

zrss commented Jun 10, 2024

what is the diff of gorm tag - and -:all

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

the above doc explains the - gorm tag

  • - indicates that gorm ignore this filed, no read/write permission, but -:all indicates that gorm ignore read/write/migrate.

so i doubt about the diff between - and -:all, and do a simple experiment.

step 1, create the user model.

package main

import (
	"fmt"
	"gorm.io/gorm"
	"time"

	"github.com/sirupsen/logrus"
	"gorm.io/driver/postgres"
	"gorm.io/gorm/logger"
)

type User struct {
	ID   uint
	Name string
	Age  int
}

func main() {
	gormLogger := logger.New(
		logrus.WithField("Component", "gorm"),
		logger.Config{
			SlowThreshold:             time.Second,
			LogLevel:                  logger.Warn,
			IgnoreRecordNotFoundError: true,
			ParameterizedQueries:      true,
			Colorful:                  false,
		},
	)

	dbArgs := ...

	db, err := gorm.Open(postgres.Open(dbArgs), &gorm.Config{Logger: gormLogger})
	if err != nil {
		logrus.Fatal(err)
	}

	err = db.AutoMigrate(&User{})
	if err != nil {
		logrus.Warning(err)
	}
}

currently, the result in postgres db shows that the Age column is int8.

step 2, change the Age field of User model from int to string type, and add only the gorm tag -.

package main

import (
	"fmt"
	"gorm.io/gorm"
	"time"

	"github.com/sirupsen/logrus"
	"gorm.io/driver/postgres"
	"gorm.io/gorm/logger"
)

type User struct {
	ID   uint
	Name string
	Age  string `gorm:"-"`
}

func main() {
	gormLogger := logger.New(
		logrus.WithField("Component", "gorm"),
		logger.Config{
			SlowThreshold:             time.Second,
			LogLevel:                  logger.Warn,
			IgnoreRecordNotFoundError: true,
			ParameterizedQueries:      true,
			Colorful:                  false,
		},
	)

	dbArgs := ...

	db, err := gorm.Open(postgres.Open(dbArgs), &gorm.Config{Logger: gormLogger})
	if err != nil {
		logrus.Fatal(err)
	}

	err = db.AutoMigrate(&User{})
	if err != nil {
		logrus.Warning(err)
	}
}

but the result in postgres db shows that the Age column is still int8, nothing happen. so it seems gorm - tag also ignore migrate either.

step 3, remove the gorm - tag, and re-run above code, results that the Age column changed to the text type

The document you expected this should be explained

Expected answer

could someone explain in detail about the diff between - and -:all tag, or give some demo code to illustrate it may be more understandable.

@zrss zrss added the type:question general questions label Jun 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:question general questions
Projects
None yet
Development

No branches or pull requests

2 participants