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

Support for a Foreign Key referencing a Composite Primary Key #205

Open
frobones opened this issue Mar 23, 2021 · 2 comments
Open

Support for a Foreign Key referencing a Composite Primary Key #205

frobones opened this issue Mar 23, 2021 · 2 comments

Comments

@frobones
Copy link

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
}
@mohakamg
Copy link

mohakamg commented Nov 2, 2021

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

@ak89224
Copy link

ak89224 commented Mar 29, 2022

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

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

3 participants