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

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

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
  1. type User struct {
  2. gorm.Model
  3. Email *string
  4. Name string
  5. ...
  6. }
  7. type Profile struct {
  8. gorm.Model
  9. Phone string
  10. Address string
  11. ...
  12. }
  13. type Category struct {
  14. gorm.Model
  15. Name string
  16. }
  17. </details>
  18. # 答案1
  19. **得分**: 2
  20. 对于`User`拥有一个`Profile`的关系:
  21. ```go
  22. type User struct {
  23. gorm.Model
  24. Email *string
  25. Name string
  26. Profile Profile // 这是关键的不同之处
  27. }
  28. type Profile struct {
  29. gorm.Model
  30. UserId int // 这是重要的
  31. Phone string
  32. Address string
  33. }

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

  1. type User struct {
  2. gorm.Model
  3. Email *string
  4. Name string
  5. }
  6. type Profile struct {
  7. gorm.Model
  8. UserId int // 这是重要的
  9. User User // 这是关键的不同之处
  10. Phone string
  11. Address string
  12. }

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

  1. type User struct {
  2. gorm.Model
  3. Email *string
  4. Name string
  5. CategoryList []Category
  6. }
  7. type Category struct {
  8. gorm.Model
  9. UserId int // 这是重要的
  10. Name string
  11. }

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

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

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

For User Has One Profile

  1. type User struct {
  2. gorm.Model
  3. Email *string
  4. Name string
  5. Profile Profile //this is the key different
  6. }
  7. type Profile struct {
  8. gorm.Model
  9. UserId int //this is important
  10. Phone string
  11. Address string
  12. }

For Profile Belong To User

  1. type User struct {
  2. gorm.Model
  3. Email *string
  4. Name string
  5. }
  6. type Profile struct {
  7. gorm.Model
  8. UserId int //this is important
  9. User User //this is the key different
  10. Phone string
  11. Address string
  12. }

For User Has Many Category

  1. type User struct {
  2. gorm.Model
  3. Email *string
  4. Name string
  5. CategoryList []Category
  6. }
  7. type Category struct {
  8. gorm.Model
  9. UserId int //this is important
  10. Name string
  11. }

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

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

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:

确定