#  [ ![GORM](/gorm.svg) ](/)

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

__

__ English  English  简体中文  Deutsch  bahasa Indonesia  日本語  Русский  한국어
हिन्दी  French  Italiano  Español

#  Constraints

[ __ ](https://github.com/go-
gorm/gorm.io/edit/master/pages/docs/constraints.md "Improve this page")

GORM allows create database constraints with tag, constraints will be created
when [ AutoMigrate or CreateTable with GORM ](migration.html)

##  CHECK Constraint

Create CHECK constraints with tag ` check `

    
    
    type UserIndex struct {  
      Name  string `gorm:"check:name_checker,name <> 'jinzhu'"`  
      Name2 string `gorm:"check:name <> 'jinzhu'"`  
      Name3 string `gorm:"check:,name <> 'jinzhu'"`  
    }  
      
  
---  
  
##  Index Constraint

Checkout [ Database Indexes ](indexes.html)

##  Foreign Key Constraint

GORM will creates foreign keys constraints for associations, you can disable
this feature during initialization:

    
    
    db, err := gorm.Open(sqlite.Open("gorm.db"), &gorm.Config{  
      DisableForeignKeyConstraintWhenMigrating: true,  
    })  
      
  
---  
  
GORM allows you setup FOREIGN KEY constraints’s ` OnDelete ` , ` OnUpdate `
option with tag ` constraint ` , for example:

    
    
    type User struct {  
      gorm.Model  
      CompanyID  int  
      Company    Company    `gorm:"constraint:OnUpdate:CASCADE,OnDelete:SET NULL;"`  
      CreditCard CreditCard `gorm:"constraint:OnUpdate:CASCADE,OnDelete:SET NULL;"`  
    }  
      
    type CreditCard struct {  
      gorm.Model  
      Number string  
      UserID uint  
    }  
      
    type Company struct {  
      ID   int  
      Name string  
    }  
      
  
---  
  
[ ![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  ](indexes.html "Indexes") [ Next  __
](composite_primary_key.html "Composite Primary Key")

#  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. CHECK Constraint 
  2. Index Constraint 
  3. Foreign Key Constraint 

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