...

/

Simple Queries with Beego ORM

Simple Queries with Beego ORM

Learn to write simple queries using Beego ORM including insert, update, delete, and retrieval.

The insert, update, and delete operations

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

Press + to interact
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:

Press + to interact
// 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 ...