protoc-gen-gorm icon indicating copy to clipboard operation
protoc-gen-gorm copied to clipboard

gorm v2 and gorm v1

Open yangwawa0323 opened this issue 2 years ago • 5 comments

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
}

yangwawa0323 avatar Sep 26 '22 13:09 yangwawa0323

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

mavericksy avatar Nov 27 '22 10:11 mavericksy

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'

radaren avatar Apr 03 '23 03:04 radaren

  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

mdamt avatar Jun 26 '23 14:06 mdamt

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 [email protected] repo.

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

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

EysonZhao avatar Jul 07 '23 03:07 EysonZhao

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

AttilaTheFun avatar Feb 05 '24 21:02 AttilaTheFun