Permalink
..
Failed to load latest commit information.
README.md Fix the quick start section of the orm/README.md Aug 11, 2017
cmd.go add go simple support Mar 17, 2017
cmd_utils.go Proposal to #2952 Nov 30, 2017
db.go Proposal to #2952 Nov 30, 2017
db_alias.go Update for MySQL timezone detection bug Oct 26, 2017
db_mysql.go Proposal to #2952 Nov 30, 2017
db_oracle.go Proposal to #2952 Nov 30, 2017
db_postgres.go Proposal to #2952 Nov 30, 2017
db_sqlite.go Proposal to #2952 Nov 30, 2017
db_tables.go add go simple support Mar 17, 2017
db_tidb.go Orm: Support TiDB Sep 17, 2015
db_utils.go Add strong relationship support to orm Jan 11, 2017
models.go misc: fix typos Oct 17, 2017
models_boot.go Merge branch 'master' into master Oct 17, 2017
models_fields.go orm: 修复logic enum因为type enum改变而产生的位错位。 Nov 30, 2017
models_info_f.go Proposal to #2952 Nov 30, 2017
models_info_m.go add go simple support Mar 17, 2017
models_test.go Proposal to #2952 Nov 30, 2017
models_utils.go Proposal to #2952 Nov 30, 2017
orm.go Table not found spelling fixes May 15, 2017
orm_conds.go resolves #2291, introduces AndNotCond/OrNotCond to orm.Condition Nov 28, 2016
orm_log.go Revert "should use time.Since instead of time.Now().Sub" Sep 8, 2017
orm_object.go orm: fix miss pk when pk is negative Mar 17, 2016
orm_querym2m.go add go simple support Mar 17, 2017
orm_queryset.go Add GetCond func to querySet Jan 18, 2017
orm_raw.go 允许o.Raw(sql).QueryRows(&container) 传入的container包含结构的嵌套 May 12, 2017
orm_test.go add test case that used nested struct test QueryRows May 14, 2017
qb.go Add support "SELECT FOR UPDATE" to orm. Resolve issue #2157 Sep 12, 2016
qb_mysql.go Add support "SELECT FOR UPDATE" to orm. Resolve issue #2157 Sep 12, 2016
qb_tidb.go Add support "SELECT FOR UPDATE" to orm. Resolve issue #2157 Sep 12, 2016
types.go Add GetCond func to querySet Jan 18, 2017
utils.go Merge branch 'develop' into add-gosimple Mar 18, 2017
utils_test.go Modify func camelString to be more robust Dec 25, 2016

README.md

beego orm

Build Status

A powerful orm framework for go.

It is heavily influenced by Django ORM, SQLAlchemy.

Support Database:

Passed all test, but need more feedback.

Features:

  • full go type support
  • easy for usage, simple CRUD operation
  • auto join with relation table
  • cross DataBase compatible query
  • Raw SQL query / mapper without orm model
  • full test keep stable and strong

more features please read the docs

Install:

go get github.com/astaxie/beego/orm

Changelog

  • 2013-08-19: support table auto create
  • 2013-08-13: update test for database types
  • 2013-08-13: go type support, such as int8, uint8, byte, rune
  • 2013-08-13: date / datetime timezone support very well

Quick Start

Simple Usage

package main

import (
	"fmt"
	"github.com/astaxie/beego/orm"
	_ "github.com/go-sql-driver/mysql" // import your used driver
)

// Model Struct
type User struct {
	Id   int    `orm:"auto"`
	Name string `orm:"size(100)"`
}

func init() {
	// register model
	orm.RegisterModel(new(User))

	// set default database
	orm.RegisterDataBase("default", "mysql", "root:root@/my_db?charset=utf8", 30)
	
	// create table
	orm.RunSyncdb("default", false, true)	
}

func main() {
	o := orm.NewOrm()

	user := User{Name: "slene"}

	// insert
	id, err := o.Insert(&user)

	// update
	user.Name = "astaxie"
	num, err := o.Update(&user)

	// read one
	u := User{Id: user.Id}
	err = o.Read(&u)

	// delete
	num, err = o.Delete(&u)	
}

Next with relation

type Post struct {
	Id    int    `orm:"auto"`
	Title string `orm:"size(100)"`
	User  *User  `orm:"rel(fk)"`
}

var posts []*Post
qs := o.QueryTable("post")
num, err := qs.Filter("User__Name", "slene").All(&posts)

Use Raw sql

If you don't like ORM,use Raw SQL to query / mapping without ORM setting

var maps []Params
num, err := o.Raw("SELECT id FROM user WHERE name = ?", "slene").Values(&maps)
if num > 0 {
	fmt.Println(maps[0]["id"])
}

Transaction

o.Begin()
...
user := User{Name: "slene"}
id, err := o.Insert(&user)
if err == nil {
	o.Commit()
} else {
	o.Rollback()
}

Debug Log Queries

In development env, you can simple use

func main() {
	orm.Debug = true
...

enable log queries.

output include all queries, such as exec / prepare / transaction.

like this:

[ORM] - 2013-08-09 13:18:16 - [Queries/default] - [    db.Exec /     0.4ms] - [INSERT INTO `user` (`name`) VALUES (?)] - `slene`
...

note: not recommend use this in product env.

Docs

more details and examples in docs and test

documents