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

panic with sql:"-" field set #945

Closed
Tommy-42 opened this issue Aug 9, 2018 · 6 comments
Closed

panic with sql:"-" field set #945

Tommy-42 opened this issue Aug 9, 2018 · 6 comments

Comments

@Tommy-42
Copy link

Tommy-42 commented Aug 9, 2018

Hello,

I am using golang/protobuf in one of my project which has an older version that another one that I am currently working on ( which has the very last one ).

For some reason, they implemented some "breaking change" to the generated code ( they add 3 more fields to the generated struct.

for example :

message User {
  string id = 1;
  string username = 2;
}

generated :

type User struct {
	Id                   string           `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"`
	Username             string           `protobuf:"bytes,2,opt,name=username,proto3" json:"username,omitempty"`
}

but now generates :

type User struct {
	Id                   string           `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"`
	Username             string           `protobuf:"bytes,2,opt,name=username,proto3" json:"username,omitempty"`
	XXX_NoUnkeyedLiteral struct{}         `json:"-"`
	XXX_unrecognized     []byte           `json:"-"`
	XXX_sizecache        int32            `json:"-"`
}

So you may start understanding what problem it could cause with go-pg ... :(

I do have found a way to append sql:"-" tag with github.com/favadi/protoc-go-inject-tag
to the struct

	XXX_NoUnkeyedLiteral struct{}         `json:"-" sql:"-"`
	XXX_unrecognized     []byte           `json:"-" sql:"-"`
	XXX_sizecache        int32            `json:"-" sql:"-"`
}

but when I do :

err = s.DB.Model(user).Where("username = ?", req.Username).Select()

I've got a panic :

panic: reflect: call of reflect.Value.Field on zero Value [recovered]
	panic: reflect: call of reflect.Value.Field on zero Value

goroutine 21 [running]:
testing.tRunner.func1(0xc4200fef00)
	/usr/local/Cellar/go/1.10/libexec/src/testing/testing.go:742 +0x29d
panic(0x15aa680, 0xc4200c98c0)
	/usr/local/Cellar/go/1.10/libexec/src/runtime/panic.go:505 +0x229
reflect.Value.Field(0x0, 0x0, 0x0, 0x0, 0xc4201fcd70, 0x2, 0xc420114000)
	/usr/local/Cellar/go/1.10/libexec/src/reflect/value.go:782 +0x11b
myrepo/vendor/github.com/go-pg/pg/orm.fieldByIndex(0x0, 0x0, 0x0, 0xc4201fc3e8, 0x1, 0x1, 0x166b705, 0x2, 0xc4200bb3f0)
	myrepo/vendor/github.com/go-pg/pg/orm/util.go:54 +0x4c
myrepo/vendor/github.com/go-pg/pg/orm.(*Field).ScanValue(0xc4200de120, 0x0, 0x0, 0x0, 0xc420114000, 0x24, 0x200, 0x16d5280, 0xc4200ca0c0)
	myrepo/vendor/github.com/go-pg/pg/orm/field.go:86 +0x6c
myrepo/vendor/github.com/go-pg/pg/orm.(*structTableModel).scanColumn(0xc420174100, 0x0, 0xc4201fcd70, 0x2, 0xc420114000, 0x24, 0x200, 0x24, 0x0, 0x0)
	myrepo/vendor/github.com/go-pg/pg/orm/model_table_struct.go:237 +0x123
myrepo/vendor/github.com/go-pg/pg/orm.(*structTableModel).ScanColumn(0xc420174100, 0x0, 0xc4201fcd70, 0x2, 0xc420114000, 0x24, 0x200, 0x200, 0x0)
	myrepo/vendor/github.com/go-pg/pg/orm/model_table_struct.go:199 +0x8e
myrepo/vendor/github.com/go-pg/pg.readDataRow(0xc420128000, 0x16d5600, 0xc420174100, 0xc420240120, 0x6, 0x6, 0x6, 0x0)
	myrepo/vendor/github.com/go-pg/pg/messages.go:590 +0x15f
myrepo/vendor/github.com/go-pg/pg.readSimpleQueryData(0xc420128000, 0x16577a0, 0xc420174100, 0x1617760, 0xc4200c9760, 0xc4200ab050)
	myrepo/vendor/github.com/go-pg/pg/messages.go:636 +0x25e
myrepo/vendor/github.com/go-pg/pg.(*DB).simpleQueryData(0xc4201bc230, 0xc420128000, 0x16577a0, 0xc420174100, 0x1617760, 0xc4200c9760, 0xc4200ab050, 0x1, 0x1, 0x1d, ...)
	myrepo/vendor/github.com/go-pg/pg/db.go:469 +0xe0
myrepo/vendor/github.com/go-pg/pg.(*DB).Query(0xc4201bc230, 0x16577a0, 0xc420174100, 0x1617760, 0xc4200c9760, 0xc4200ab050, 0x1, 0x1, 0xc420069d48, 0x1012a98, ...)
	myrepo/vendor/github.com/go-pg/pg/db.go:256 +0x170
myrepo/vendor/github.com/go-pg/pg.(*DB).QueryOne(0xc4201bc230, 0x16577a0, 0xc420174100, 0x1617760, 0xc4200c9760, 0xc4200ab050, 0x1, 0x1, 0xc420069dd0, 0x100feaf, ...)
	myrepo/vendor/github.com/go-pg/pg/db.go:281 +0x8d
myrepo/vendor/github.com/go-pg/pg/orm.(*Query).query(0xc420095680, 0x16e0000, 0xc420174100, 0x1617760, 0xc4200c9760, 0xc420174100, 0x0, 0x0, 0xc4200b70c0)
	myrepo/vendor/github.com/go-pg/pg/orm/query.go:622 +0x12d
myrepo/vendor/github.com/go-pg/pg/orm.(*Query).Select(0xc420095680, 0x0, 0x0, 0x0, 0x1, 0x1)
	myrepo/vendor/github.com/go-pg/pg/orm/query.go:594 +0xf9
myrepo/service/users/v0.(*Service).AuthenticateUser(0xc4200c2678, 0x16dc480, 0xc4200b4048, 0xc4200b79c0, 0x10e60fd, 0x17a8108, 0xc420045798)
	myrepo/service/users/v0/users.go:49 +0x2b1
myrepo/service/users/v0.TestAuthenticateUser.func1(0xc4200fef00)
	myrepo/service/users/v0/users_test.go:104 +0x64
testing.tRunner(0xc4200fef00, 0xc420232460)
	/usr/local/Cellar/go/1.10/libexec/src/testing/testing.go:777 +0xd0
created by testing.(*T).Run
	/usr/local/Cellar/go/1.10/libexec/src/testing/testing.go:824 +0x2e0

the panic happens when I add sql:"-" to the struct, If I remove it, I have the famous
ERROR #42703 column user.xxx__no_unkeyed_literal does not exist (addr="127.0.0.1:5432")

do you have an idea on how to resolve this ?

I may have done something wrong ...

thanks for your help

@vmihailenco
Copy link
Member

Hi,

I can't reproduce this on latest master. Can you to upgrade go-pg and create a reproducer?

@Tommy-42
Copy link
Author

Hi, dep init has set up a specific version

 [[constraint]]
  name = "github.com/go-pg/pg"
  version = "6.14.3"

I will try on master branch and if I do have the same behavior, I will try to setup a reproducer

@Tommy-42
Copy link
Author

After updating, It works, I no longer have the panic.

Thanks

@Tommy-42
Copy link
Author

Arf mybad, I did it too fast, my db was empty so no data were found, after adding some data, it actually fails again :/ I will try to set up a reproducer

@Tommy-42
Copy link
Author

https://github.com/Tommy-42/go-pg-error-reproducer

It should fail with the same error

@vmihailenco
Copy link
Member

I will fix the panic by returning an error, but overall you should initialize the user on https://github.com/Tommy-42/go-pg-error-reproducer/blob/master/main.go#L47, e.g. via user := new(User).

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

2 participants