How to create one to one relationship and foreign key in gorm?

huangapple go评论86阅读模式
英文:

How to create one to one relationship and foreign key in gorm?

问题

Has One, Has Many, and Belongs To are different types of relationships in database modeling, specifically in the context of object-relational mapping (ORM) frameworks like GORM.

  1. Has One: This relationship indicates that one model has a single associated model. In your example, the Profile model has a Has One relationship with the User model, meaning that each Profile instance is associated with one and only one User instance. This relationship is typically represented by a foreign key in the associated model's table.

  2. Has Many: This relationship indicates that one model has multiple associated models. In your example, there is no explicit Has Many relationship defined, but it could be added if, for example, a User can have multiple Profile instances. This relationship is typically represented by a foreign key in the associated model's table.

  3. Belongs To: This relationship indicates that one model belongs to another model. In your example, the Category model has a Belongs To relationship with the User model, meaning that each Category instance is associated with one and only one User instance. This relationship is typically represented by a foreign key in the model's table that belongs to another model.

To summarize, Has One represents a one-to-one relationship, Has Many represents a one-to-many relationship, and Belongs To represents a many-to-one relationship.

英文:

What is the difference between Has One, Has Many and Belong To

I have 3 Models

  • User
  • Profile Where profile and user should have one to one relationship
  • Category Where category should be foreign key to user
type User struct {
gorm.Model
Email *string
Name string
...
}

type Profile struct {
gorm.Model
Phone string
Address string
...

}

type Category struct {
gorm.Model
Name string

}

</details>


# 答案1
**得分**: 2

对于`User`拥有一个`Profile`的关系:

```go
type User struct {
 gorm.Model
 Email *string
 Name string
 Profile Profile // 这是关键的不同之处
}
type Profile struct {
 gorm.Model
 UserId int // 这是重要的
 Phone string
 Address string
}

对于Profile属于一个User的关系:

type User struct {
   gorm.Model
   Email *string
   Name string
}
type Profile struct {
   gorm.Model
   UserId int // 这是重要的
   User User // 这是关键的不同之处
   Phone string
   Address string
}

对于User拥有多个Category的关系:

type User struct {
   gorm.Model
   Email *string
   Name string
   CategoryList []Category
}
type Category struct {
   gorm.Model
   UserId int // 这是重要的
   Name string
}

编辑:UserId字段将成为你的外键。

如果你希望gorm自动为你创建表格,你可以在main.go中使用AutoMigrate

err := db.AutoMigrate(your_model_package.User{})
if err != nil {
    return err
}
英文:

For User Has One Profile

type User struct {
   gorm.Model
   Email *string
   Name string
   Profile Profile //this is the key different
}
type Profile struct {
   gorm.Model
   UserId int //this is important
   Phone string
   Address string
}

For Profile Belong To User

type User struct {
   gorm.Model
   Email *string
   Name string
}
type Profile struct {
   gorm.Model
   UserId int //this is important
   User User //this is the key different
   Phone string
   Address string
}

For User Has Many Category

type User struct {
   gorm.Model
   Email *string
   Name string
   CategoryList []Category
}
type Category struct {
   gorm.Model
   UserId int //this is important
   Name string
}

Edit: UserId field will become your foreign key.

If you want gorm to automatically create table for you, you can use AutoMigrate in main.go

err := db.AutoMigrate(your_model_package.User{})
if err != nil {
	return err
}

huangapple
  • 本文由 发表于 2022年5月30日 18:44:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/72433044.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定