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

gorm v2 and gorm v1 #243

Open
yangwawa0323 opened this issue Sep 26, 2022 · 5 comments
Open

gorm v2 and gorm v1 #243

yangwawa0323 opened this issue Sep 26, 2022 · 5 comments

Comments

@yangwawa0323
Copy link

By using protoc-gen-gorm generated the *.pb.gorm.go file, the problem is my project of the protobuf service using the "gorm.io/gorm" is v2 of the gorm project that is not compatible of the "github.com/jinzhu/gorm" which is v1.

After I change the alias gorm1 of imported package to v2 in the generated code *.pb.gorm.go, the VS code raised a lot of error.

import (
        ...
	gorm1 "github.com/jinzhu/gorm"
	_ "gorm.io/driver/sqlite"
)
func (us *UserServer) CreateUser(ctx context.Context,
	req *pb_user.CreateUserRequest) (*pb_user.CreateUserResponse, error) {

	var Id string

	storeType := reflect.TypeOf(us.Store)

	switch storeType.String() {
	case "*user.UserDBStore":
		fmt.Println("[DEBUG] : store type is user.UserDBStore")
		// if UserServer's Store is gorm.DB
		if _, ok := us.Store.(*UserDBStore); ok {
			db, err := gorm1.Open("sqlite3", "test.db")
			if err != nil {
				log.Fatal(out.Panic("cannot connect to MySQL database: %v", err))
				return nil, err
			}
			log.Println("Use the protoc gorm DefaultCreateUser func")
			user, err := pb_user.DefaultCreateUser(ctx, req.User, db)
			Id = user.String()

		}
	default:
		user, err := req.GetUser().ToORM(ctx)
		log.Print(out.Debug("Get user request: %#v", user))
		if err != nil {
			return nil, err
		}
		fmt.Println("use the customize gorm UserServer.Store to create user")
		if err = us.Store.Save(&user); err != nil {
			log.Fatal(out.Panic("Cannot save the user to DB : %#v", err))
		}
		fmt.Fprint(os.Stdout, out.Debug("user is created : %#v\n", user))
		Id = user.Id
	}

	return &pb_user.CreateUserResponse{
		Id: Id,
	}, nil
}
@mavericksy
Copy link

It appears this problem stems from the Atlas app toolkit.
Their collection operators using jinzhu gorm v1

https://github.com/infobloxopen/atlas-app-toolkit/blob/2a142f36286edfc9d789274c7c8ff1f903163a67/gorm/collection_operators.go#L219

@radaren
Copy link

radaren commented Apr 3, 2023

Using this plugin, I occurs some incompatibility problems on gorm v2 version:

  1. deleted at: https://gorm.io/docs/delete.html#Soft-Delete, v2 use gorm.io/plugin/soft_delete to annotate soft delete
  2. unique index: https://gorm.io/docs/indexes.html#uniqueIndex, v2 name is 'uniqueIndex' rathar than 'unique_index'

@mdamt
Copy link

mdamt commented Jun 26, 2023

  1. Run go get github.com/infobloxopen/atlas-app-toolkit@gorm_v2 and go install in this repo
  2. Run go get github.com/infobloxopen/atlas-app-toolkit@gorm_v2 in your repo using the generated pb.go

@EysonZhao
Copy link

I met same problem results to two versions of gorm in our project go mod file. I finally resolve it by using latest version of code.
The problem is, current code is not the release version (may be not stable? ). The latest version still keeps v1.1.2 (released 2022 and do not contains gorm v2). You need to go get latest commit manually by targeting to commit id:

  1. In your go repo, run

go get github.com/infobloxopen/protoc-gen-gorm@cedaaf0105d2
It will be auto added to your go.mod file, mod's name will be: github.com/infobloxopen/protoc-gen-gorm v1.1.3-0.20230222001438-cedaaf0105d2

  1. Make sure to do not use your v1.1.2 version, suggest to delete local protoc-gen-gorm@v1.1.2 repo.

  2. cd to the downloaded v1.1.3 repo, and run:
    go install

  3. Re-generate your .pb.go code by protoc xxx

@AttilaTheFun
Copy link

It would be great to cut a v1.1.3 (or a v2) version so people don't have to do this manually

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants