label=Latest GORM Release&color=red&&style=for-the-
badge&logo=go&logoColor=red) ](v2_release_note.html)

Last updated: 2024-04-13  [ __ Prev  ](has_many.html "Has Many") [ Next  __
](associations.html "Association Mode")

#  Platinum Sponsors __

[ Become a Sponsor! ](/contribute.html#Donations)

#  Gold Sponsors __

[ ![Encore](/sponsors-imgs/encore.png)
](https://www.encore.dev/?utm_source=github&utm_medium=github-gorm-sponsor-
logo&utm_campaign=gorm-20231215 "Encore")

[ ![Incident.io](/sponsors-imgs/incident-io.svg) ](https://incident.io
"Incident.io")

[ Become a Sponsor! ](/contribute.html#Donations)

#  Platinum Sponsors

[ Become a Sponsor! ](/contribute.html#Donations)

#  Gold Sponsors

[ ![Encore](/sponsors-imgs/encore.png)
](https://www.encore.dev/?utm_source=github&utm_medium=github-gorm-sponsor-
logo&utm_campaign=gorm-20231215 "Encore")

[ ![Incident.io](/sponsors-imgs/incident-io.svg) ](https://incident.io
"Incident.io")

[ Become a Sponsor! ](/contribute.html#Donations)

**Contents**

  1. Many To Many 
  2. Back-Reference 
    1. Declare 
    2. Retrieve 
  3. Override Foreign Key 
  4. Self-Referential Many2Many 
  5. Eager Loading 
  6. CRUD with Many2Many 
  7. Customize JoinTable 
  8. FOREIGN KEY Constraints 
  9. Composite Foreign Keys 

[ Improve this page ](https://github.com/go-
gorm/gorm.io/edit/master/pages/docs/many_to_many.md) Back to Top

**Getting Started** [ Overview ](index.html) [ Declaring Models ](models.html)
[ Connecting to Database ](connecting_to_the_database.html) **CRUD Interface**
[ Create ](create.html) [ Query ](query.html) [ Advanced Query
](advanced_query.html) [ Update ](update.html) [ Delete ](delete.html) [ Raw
SQL & SQL Builder ](sql_builder.html) **Associations** [ Belongs To
](belongs_to.html) [ Has One ](has_one.html) [ Has Many ](has_many.html) [
Many To Many ](many_to_many.html) [ Association Mode ](associations.html) [
Preloading (Eager Loading) ](preload.html) **Tutorials** [ Context
](context.html) [ Error Handling ](error_handling.html) [ Method Chaining
](method_chaining.html) [ Session ](session.html) [ Hooks ](hooks.html) [
Transactions ](transactions.html) [ Migration ](migration.html) [ Logger
](logger.html) [ Generic Database Interface ](generic_interface.html) [
Performance ](performance.html) [ Customize Data Types ](data_types.html) [
Scopes ](scopes.html) [ Conventions ](conventions.html) [ Settings
](settings.html) **Advanced Topics** [ Database Resolver ](dbresolver.html) [
Sharding ](sharding.html) [ Serializer ](serializer.html) [ Prometheus
](prometheus.html) [ Hints ](hints.html) [ Indexes ](indexes.html) [
Constraints ](constraints.html) [ Composite Primary Key
](composite_primary_key.html) [ Security ](security.html) [ GORM Config
](gorm_config.html) [ Write Plugins ](write_plugins.html) [ Write Driver
](write_driver.html) [ ChangeLog ](changelog.html) [ Community
](/community.html) [ Contribute ](/contribute.html) [ Translate current site
](/contribute.html#Translate-this-site)

© 2013~2024 [ Jinzhu ](https://github.com/jinzhu)  
Documentation licensed under [ CC BY 4.0
](http://creativecommons.org/licenses/by/4.0/) .  

感谢 [ 无闻 ](https://github.com/unknwon) 对域名 [ gorm.cn ](https://gorm.cn) 的捐赠  
[ 浙ICP备2020033190号-1 ](http://beian.miit.gov.cn/)  

[ __ ](https://twitter.com/zhangjinzhu) [ __ ](https://github.com/go-
gorm/gorm)

  * [ __ Home ](/)
[ __ Docs ](/docs/) [ __ Gen ](/gen/) [ __ Community ](/community.html) [ __
API ](https://pkg.go.dev/gorm.io/gorm) [ __ Contribute ](/contribute.html)

**Getting Started** [ Overview ](index.html) [ Declaring Models ](models.html)
[ Connecting to Database ](connecting_to_the_database.html) **CRUD Interface**
[ Create ](create.html) [ Query ](query.html) [ Advanced Query
](advanced_query.html) [ Update ](update.html) [ Delete ](delete.html) [ Raw
SQL & SQL Builder ](sql_builder.html) **Associations** [ Belongs To
](belongs_to.html) [ Has One ](has_one.html) [ Has Many ](has_many.html) [
Many To Many ](ma