Many To Many

Many to Many add a join table between two models.

For example, if your application includes users and languages, and a user can speak many languages, and many users can speak a specified language.

  1. // User has and belongs to many languages, `user_languages` is the join table
  2. type User struct {
  3. gorm.Model
  4. Languages []Language `gorm:"many2many:user_languages;"`
  5. }
  6. type Language struct {
  7. gorm.Model
  8. Name string
  9. }

When using GORM AutoMigrate to create a table for User, GORM will create the join table automatically

Back-Reference

  1. // User has and belongs to many languages, use `user_languages` as join table
  2. type User struct {
  3. gorm.Model
  4. Languages []*Language `gorm:"many2many:user_languages;"`
  5. }
  6. type Language struct {
  7. gorm.Model
  8. Name string
  9. Users []*User `gorm:"many2many:user_languages;"`
  10. }

Override Foreign Key

For a many2many relationship, the join table owns the foreign key which references two models, for example:

  1. type User struct {
  2. gorm.Model
  3. Languages []Language `gorm:"many2many:user_languages;"`
  4. }
  5. type Language struct {
  6. gorm.Model
  7. Name string
  8. }
  9. // Join Table: user_languages
  10. // foreign key: user_id, reference: users.id
  11. // foreign key: language_id, reference: languages.id

To override them, you can use tag foreignKey, references, joinForeignKey, joinReferences, not necessary to use them together, you can just use one of them to override some foreign keys/references

  1. type User struct {
  2. gorm.Model
  3. Profiles []Profile `gorm:"many2many:user_profiles;foreignKey:Refer;joinForeignKey:UserReferID;References:UserRefer;joinReferences:ProfileRefer"`
  4. Refer uint `gorm:"index:,unique"`
  5. }
  6. type Profile struct {
  7. gorm.Model
  8. Name string
  9. UserRefer uint `gorm:"index:,unique"`
  10. }
  11. // Which creates join table: user_profiles
  12. // foreign key: user_refer_id, reference: users.refer
  13. // foreign key: profile_refer, reference: profiles.user_refer

NOTE:
Some databases only allow create database foreign keys that reference on a field having unique index, so you need to specify the unique index tag if you are creating database foreign keys when migrating

Self-Referential Many2Many

Self-referencing many2many relationship

  1. type User struct {
  2. gorm.Model
  3. Friends []*User `gorm:"many2many:user_friends"`
  4. }
  5. // Which creates join table: user_friends
  6. // foreign key: user_id, reference: users.id
  7. // foreign key: friend_id, reference: users.id

Eager Loading

GORM allows eager loading has many associations with Preload, refer Preloading (Eager loading) for details

CRUD with Many2Many

Please checkout Association Mode for working with many2many relations

Customize JoinTable

JoinTable can be a full-featured model, like having Soft DeleteHooks supports and more fields, you can setup it with SetupJoinTable, for example:

NOTE:
Customized join table’s foreign keys required to be composited primary keys or composited unique index

  1. type Person struct {
  2. ID int
  3. Name string
  4. Addresses []Address `gorm:"many2many:person_addresses;"`
  5. }
  6. type Address struct {
  7. ID uint
  8. Name string
  9. }
  10. type PersonAddress struct {
  11. PersonID int `gorm:"primaryKey"`
  12. AddressID int `gorm:"primaryKey"`
  13. CreatedAt time.Time
  14. DeletedAt gorm.DeletedAt
  15. }
  16. func (PersonAddress) BeforeCreate(db *gorm.DB) error {
  17. // ...
  18. }
  19. // Change model Person's field Addresses' join table to PersonAddress
  20. // PersonAddress must defined all required foreign keys or it will raise error
  21. err := db.SetupJoinTable(&Person{}, "Addresses", &PersonAddress{})

FOREIGN KEY Constraints

You can setup OnUpdate, OnDelete constraints with tag constraint, it will be created when migrating with GORM, for example:

  1. type User struct {
  2. gorm.Model
  3. Languages []Language `gorm:"many2many:user_speaks;"`
  4. }
  5. type Language struct {
  6. Code string `gorm:"primarykey"`
  7. Name string
  8. }
  9. // CREATE TABLE `user_speaks` (`user_id` integer,`language_code` text,PRIMARY KEY (`user_id`,`language_code`),CONSTRAINT `fk_user_speaks_user` FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON DELETE SET NULL ON UPDATE CASCADE,CONSTRAINT `fk_user_speaks_language` FOREIGN KEY (`language_code`) REFERENCES `languages`(`code`) ON DELETE SET NULL ON UPDATE CASCADE);

You are also allowed to delete selected many2many relations with Select when deleting, checkout Delete with Select for details

Composite Foreign Keys

If you are using Composite Primary Keys for your models, GORM will enable composite foreign keys by default

You are allowed to override the default foreign keys, to specify multiple foreign keys, just separate those keys’ name by commas, for example:

  1. type Tag struct {
  2. ID uint `gorm:"primaryKey"`
  3. Locale string `gorm:"primaryKey"`
  4. Value string
  5. }
  6. type Blog struct {
  7. ID uint `gorm:"primaryKey"`
  8. Locale string `gorm:"primaryKey"`
  9. Subject string
  10. Body string
  11. Tags []Tag `gorm:"many2many:blog_tags;"`
  12. LocaleTags []Tag `gorm:"many2many:locale_blog_tags;ForeignKey:id,locale;References:id"`
  13. SharedTags []Tag `gorm:"many2many:shared_blog_tags;ForeignKey:id;References:id"`
  14. }
  15. // Join Table: blog_tags
  16. // foreign key: blog_id, reference: blogs.id
  17. // foreign key: blog_locale, reference: blogs.locale
  18. // foreign key: tag_id, reference: tags.id
  19. // foreign key: tag_locale, reference: tags.locale
  20. // Join Table: locale_blog_tags
  21. // foreign key: blog_id, reference: blogs.id
  22. // foreign key: blog_locale, reference: blogs.locale
  23. // foreign key: tag_id, reference: tags.id
  24. // Join Table: shared_blog_tags
  25. // foreign key: blog_id, reference: blogs.id
  26. // foreign key: tag_id, reference: tags.id

Also check out Composite Primary Keys