Search⌘ K
AI Features

Simple Queries with Beego ORM

Explore how to use Beego ORM to manage database operations within Golang web applications. Understand how to insert, update, and delete records, as well as perform select queries with filtering, ordering, and pagination. This lesson provides practical knowledge for interacting with your data effectively using Beego's ORM features.

The insert, update, and delete operations

For the purpose of simplicity, we will use the following model structure throughout this lesson:

Go (1.18.2)
type User struct {
Id int
Name string
Age int
}

This structure represents the following:

  • Lines 1–5: The User structure is the model for the user table.

  • Lines 2–4: The Id, Name, and Age variables represent the columns id, name and age, respectively.

Insert

The following code can be used to insert a row in the user table:

Go (1.18.2)
// Insert in user table
user := User{Name: "MountainMaverick", Age: 34}
o := orm.NewOrm()
id, err := o.Insert(&user)
fmt.Println(id, err)

To insert a new record, we do the following:

  • Line 2: We create an instance of the User struct with the desired values of the row we want to insert.

  • Line 3: We create a new ORM object. This variable o is used to do ORM operations.

  • Line 4: We ...