Golang Engineering Components Object-Relational Mapping gorm's Model and the use of tags

When it comes to ORM (Object-Relational Mapping) in golang, gorm is a commonly used framework. Here, we will explore the Model definition and tag usage in gorm.

  1. Model definition

In gorm, the Model definition represents the mapping relationship between the database table and the golang structure. Basically, each database table corresponds to a golang structure.

A sample code is as follows:

type User struct {
    gorm.Model
    Name string
    Age  int
}

This example defines a struct named User and specifies the relationship between it and gorm.Model. gorm.Model contains some default fields (ID, CreatedAt, UpdatedAt, and DeletedAt) and some methods (BeforeCreate, BeforeUpdate, etc.).

  1. Tag use

Tag is the meta information added to describe the data type. In the ORM framework, Tag is usually used to describe the information of the field corresponding to the database column. In gorm, you can use tags to customize table names, column names, constraints, and other information.

Here are some common tags:

  • column: Specifies the column name.
  • type: Specifies the data type.
  • not null: Specifies that it is not empty.
  • unique: Specifies a uniqueness constraint.
  • default: Specifies the default value.
  • primary_key: Specifies the primary key.
  • auto_increment: Specifies self-growth.

Examples are as follows:

type Product struct {
    ID          uint   `gorm:"primaryKey"`
    Code        string `gorm:"uniqueIndex"`
    Price       uint
}

This example defines a Product structure, which contains ID, Code and Price fields. It also defines the ID field with tag as the primary key and Code as the unique index.

  1. In conjunction with

In gorm, the combination of Model definition and tag can realize more flexible ORM mapping. Here is a complete example:

type Product struct {
    gorm.Model
    Code  string `gorm:"uniqueIndex"`
    Price uint
}

func main() {
    db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{})
    if err != nil {
        panic("failed to connect database")
    }

    // 自动迁移 schema
    db.AutoMigrate(&Product{})

    // 创建记录
    db.Create(&Product{Code: "D42", Price: 100})

    // 查询单个记录
    var product Product
    db.First(&product, "code = ?", "D42")

    // 更新记录 - 将product的price更新为200
    db.Model(&product).Update("Price", 200)

    // 删除记录 - 删除product
    db.Delete(&product, 1)
}

This example demonstrates how to use gorm to create, query, update and delete data in a table. The Model definition in the code specifies the table name and default fields; while the tag is used to customize column names and constraint information.

Summarize

In golang, gorm provides a powerful and flexible ORM framework. The combination of Model definition and tag can easily complete the mapping relationship between database tables and golang structures, and supports automatic migration, addition, deletion, modification and query of database operations.

Guess you like

Origin blog.csdn.net/SMILY12138/article/details/130927843