name: Overview
sort: 1
Models - Beego ORM
Beego ORM is a powerful ORM framework written in Go. It is inspired by Django ORM and SQLAlchemy.
This framework is still under development so compatibility is not guaranteed.
Supported Database:
- MySQL:github.com/go-sql-driver/mysql
- PostgreSQL:github.com/lib/pq
- Sqlite3:github.com/mattn/go-sqlite3
All of the database drivers have passed the tests, but we still need your feedback and bug reports.
ORM Features:
- Supports all the types in Go.
- CRUD is easy to use.
- Auto join connection tables.
- Compatible with crossing database queries.
- Supports raw SQL query and mapping.
- Strict and well-covered test cases ensure the ORM’s stability.
You can learn more in this documentation.
Install ORM:
go get github.com/astaxie/beego/orm
Change log
- 2016-01-18: Renamed the drivers name
- 2014-03-10: GetDB Get *sql.DB from registered databases. ResetModelCache reset registered models.
2014-02-10: beego1.1.0 Change log
About Timezone
Add api: Ormer.InsertMulti Ormer.ReadOrCreate RawSeter.RowsToMap RawSeter.RowsToStruct orm.NewOrmWithDB
Modify api: RawSeter.Values support set columns RawSeter.ValuesList support set columns RawSeter.ValuesFlat support set column RawSeter.QueryRow/QueryRows changed to map struct field with column name(don’t need order by field index)
2013-10-14: Load Related Fields, Handling ManyToMany Relation, improved Relational Queries
- 2013-10-09: Atom operation updating
- 2013-09-22: RegisterDataBase maxIdle and maxConn are changed to optional params, MySQL Custom engine
- 2013-09-16: Supports maxIdle and maxConn SetMaxIdleConns / SetMaxOpenConns
- 2013-09-12: Read Supports condition fields Update / All / One Supports setting return fields
- 2013-09-09: Raw SQL QueryRow/QueryRows
- 2013-08-27: Improved Table auto generating
- 2013-08-19: Finished Table auto generating
- 2013-08-13: Updated database type tests
- 2013-08-13: Updated Go type support. E.g.: int8, uint8, byte and rune etc
- 2013-08-13: Improved timezone support for date and datetime
Quickstart
Demo
package mainimport ("fmt""github.com/astaxie/beego/orm"_ "github.com/go-sql-driver/mysql" // import your required driver)// Model Structtype User struct {Id intName string `orm:"size(100)"`}func init() {// register modelorm.RegisterModel(new(User))// set default databaseorm.RegisterDataBase("default", "mysql", "root:root@/my_db?charset=utf8")}func main() {o := orm.NewOrm()user := User{Name: "slene"}// insertid, err := o.Insert(&user)fmt.Printf("ID: %d, ERR: %v\n", id, err)// updateuser.Name = "astaxie"num, err := o.Update(&user)fmt.Printf("NUM: %d, ERR: %v\n", num, err)// read oneu := User{Id: user.Id}err = o.Read(&u)fmt.Printf("ERR: %v\n", err)// deletenum, err = o.Delete(&u)fmt.Printf("NUM: %d, ERR: %v\n", num, err)}
Relation Query
type Post struct {Id int `orm:"auto"`Title string `orm:"size(100)"`User *User `orm:"rel(fk)"`}var posts []*Postqs := o.QueryTable("post")num, err := qs.Filter("User__Name", "slene").All(&posts)
Raw SQL query
You can always use raw SQL to query and mapping.
var maps []Paramsnum, err := o.Raw("SELECT id FROM user WHERE name = ?", "slene").Values(&maps)if num > 0 {fmt.Println(maps[0]["id"])}
Transactions
o.Begin()...user := User{Name: "slene"}id, err := o.Insert(&user)if err == nil {o.Commit()} else {o.Rollback()}
Debugging query log
In development environment, you can enable debug mode by:
func main() {orm.Debug = true...
It will output every query statement including execution, preparation and transactions.
For example:
[ORM] - 2013-08-09 13:18:16 - [Queries/default] - [ db.Exec / 0.4ms] - [INSERT INTO `user` (`name`) VALUES (?)] - `slene`...
Notes: It is not recommended to enable debug mode in a production environment.

