在使用beego时遇到 need a primary key field


请问这个问题要怎么解决呢?我尝试加了 orm:"column(uid);pk" ,都还是没办法


 package models

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

type account struct {
Id int  orm:"pk;column(uid);"
username string orm:"unique;size(20);"
password string orm:"size(20)"
email string orm:"size(50)"
}

func init() {
orm.RegisterDriver("mysql", orm.DR_MySQL)

orm.RegisterDataBase("default", "mysql", "root:@/finan?charset=utf8")
orm.RegisterModel(new(account))
}

func Reg() {
o := orm.NewOrm()
var Maccount account
Maccount.username = "slene"
Maccount.password = "test"
Maccount.email = "[email protected]"

id, err := o.Insert(&Maccount)
if err == nil {
    fmt.Println(id)
}
}

beego go

野原新之助 10 years, 2 months ago

主键命名为id,试下,默认用的id的字段
比如


 type User struct {
    Id int
    Name string
}

你都不用添加field tag

Golang里导出(public)的字段必须是大写开头,字段名都改成大写吧

2P腋巫女 answered 10 years, 2 months ago

Your Answer