Golang's GORM library supports composite primary keys. But how to reference them from related models?
For example, suppose I have a User and a Note model:
type User struct {
OrganizationID uint `gorm:"primaryKey; not null"`
Name string `gorm:"primaryKey; not null"`
}
type Note struct {
ID uint `gorm:"primaryKey; not null"`
OrganizationID uint `gorm:"not null"`
UserName string `gorm:"not null"`
User User
}
The auto migrator creates the notes table like this, which fails:
CREATE TABLE "notes" ("id" bigserial NOT NULL,"user_name" text NOT NULL,"organization_id" bigint NOT NULL,PRIMARY KEY ("id"),
CONSTRAINT "fk_notes_user" FOREIGN KEY ("user_name") REFERENCES "users"("name"))
But I want it to do this instead:
CONSTRAINT "fk_notes_user" FOREIGN KEY ("user_name", "organization_id") REFERENCES "users"("name", "organization_id")
How can I accomplish this?