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 intName stringAge int}
This structure represents the following:
Lines 1–5: The
User
structure is the model for theuser
table.Lines 2–4: The
Id
,Name
, andAge
variables represent the columnsid
,name
andage
, respectively.
Insert
The following code can be used to insert a row in the user
table:
Press + to interact
// Insert in user tableuser := 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 ...