1"}})  
    // INSERT INTO `dogs` (`name`) VALUES ("dog1")  
    // INSERT INTO `toys` (`name`,`owner_id`,`owner_type`) VALUES ("toy1","1","master")  
      
  
---  
  
##  CRUD with Has One

Please checkout [ Association Mode ](associations.html#Association-Mode) for
working with ` has one ` relations

##  Eager Loading

GORM allows eager loading ` has one ` associations with ` Preload ` or ` Joins
` , refer [ Preloading (Eager loading) ](preload.html) for details

##  Self-Referential Has One

    
    
    type User struct {  
      gorm.Model  
      Name      string  
      ManagerID *uint  
      Manager   *User  
    }  
      
  
---  
  
##  FOREIGN KEY Constraints

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

    
    
    type User struct {  
      gorm.Model  
      CreditCard CreditCard `gorm:"constraint:OnUpdate:CASCADE,OnDelete:SET NULL;"`  
    }  
      
    type CreditCard struct {  
      gorm.Model  
      Number string  
      UserID uint  
    }  
      
  
---  
  
You are also allowed to delete selected has one associations with ` Select `
when deleting, checkout [ Delete with Select
](associations.html#delete_with_select) for details

[ ![GitHub tag \(latest SemVer\)](https://img.shields.io/github/v/tag/go-
gorm/gorm?label=Latest GORM Release&color=red&&style=for-the-
badge&logo=go&logoColor=red) ](v2_release_note.html)

Last updated: 2024-04-13  [ __ Prev  ](belongs_to.html "Belongs To") [ Next
__ ](has_many.html "Has Many")

#  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. Has One 
    1. Declare 
    2. Retrieve 
  2. Override Foreign Key 
  3. Override References 
  4. Polymorphism Association 
  5. CRUD with Has One 
  6. Eager Loading 
  7. Self-Referential Has One 
  8. FOREIGN KEY Constraints 

[ Improve this page ](https://github.com/go-
gorm/gorm.io/edit/master/pages/docs/has_one.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 Ke