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

Support for a Foreign Key referencing a Composite Primary Key

Open frobones opened this issue 3 years ago • 2 comments

Is it possible for a Foreign Key to reference a composite key?

I try something like this:

message Foo {
    option (gorm.opts).ormable = true;
    string id = 1;
    int64 version = 2;

    repeated Bar bars = 3 [(gorm.field).has_many = {foreignkey: "FooId,FooVersion"}];
}

message Bar {
    option (gorm.opts).ormable = true;
    string id = 1;
    int64 version = 2;
}

and the generated code produces this:

type FooORM struct {
	Bars    []*BarORM `gorm:"foreignkey:FooId,FooVersion;association_foreignkey:Id"`
	Id      string
	Version int64
}

type BarORM struct {
	FooId, FooVersion *string
	Id                string
	Version           int64
}

Is it possible to specify what the foreign keys reference, via https://gorm.io/docs/has_many.html#Override-References

Essentially, I want to generate code that looks like this:

type FooORM struct {
	Bars    []*BarORM `gorm:"foreignkey:FooId,FooVersion;References:Id,Version"`
	Id      string
	Version int64
}

type BarORM struct {
	FooId      *string
        FooVersion *int64
	Id         string
	Version    int64
}

frobones avatar Mar 23 '21 23:03 frobones

I have the same problem, have you figured it out yet?

mohakamg avatar Nov 02 '21 18:11 mohakamg

I'm also facing the same challenge, @frobones @mohakamg Hove you guys found any solution to this ?

ak89224 avatar Mar 29 '22 08:03 ak89224