如何在 GORM 中引用复合主键?
How to reference a composite primary key in GORM?
Golang 的 GORM 库支持复合主键。但是如何从相关模型中引用它们呢?
例如,假设我有一个 User 和一个 Note 模型:
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
}
自动迁移器像这样创建 notes
table,但失败了:
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"))
但我希望它改为这样做:
CONSTRAINT "fk_notes_user" FOREIGN KEY ("user_name", "organization_id") REFERENCES "users"("name", "organization_id")
我怎样才能做到这一点?
您可以使用 ForeignKey
和 References
标签。它们在 docs 中被提及,尽管在相反的 (One-to-Many) 上下文中。
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 `gorm:"ForeignKey:OrganizationID,UserName;References:OrganizationID,Name"`
}
AutoMigrate 将生成以下 sql:
CREATE TABLE `users` (`organization_id` integer NOT NULL,`name` text NOT NULL,PRIMARY KEY (`organization_id`,`name`))
CREATE TABLE `notes` (`id` integer NOT NULL,`organization_id` integer NOT NULL,`user_name` text NOT NULL,PRIMARY KEY (`id`),CONSTRAINT `fk_notes_user` FOREIGN KEY (`organization_id`,`user_name`) REFERENCES `users`(`organization_id`,`name`))
Golang 的 GORM 库支持复合主键。但是如何从相关模型中引用它们呢?
例如,假设我有一个 User 和一个 Note 模型:
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
}
自动迁移器像这样创建 notes
table,但失败了:
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"))
但我希望它改为这样做:
CONSTRAINT "fk_notes_user" FOREIGN KEY ("user_name", "organization_id") REFERENCES "users"("name", "organization_id")
我怎样才能做到这一点?
您可以使用 ForeignKey
和 References
标签。它们在 docs 中被提及,尽管在相反的 (One-to-Many) 上下文中。
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 `gorm:"ForeignKey:OrganizationID,UserName;References:OrganizationID,Name"`
}
AutoMigrate 将生成以下 sql:
CREATE TABLE `users` (`organization_id` integer NOT NULL,`name` text NOT NULL,PRIMARY KEY (`organization_id`,`name`))
CREATE TABLE `notes` (`id` integer NOT NULL,`organization_id` integer NOT NULL,`user_name` text NOT NULL,PRIMARY KEY (`id`),CONSTRAINT `fk_notes_user` FOREIGN KEY (`organization_id`,`user_name`) REFERENCES `users`(`organization_id`,`name`))