Please answer these questions before submitting your issue. Thanks!
What version of Go and beego are you using (bee version)?
go version go1.10 darwin/amd64
What operating system and processor architecture are you using (go env)?
Mac OS, Sierra
GOARCH="amd64"
What did you do?
If possible, provide a recipe for reproducing the error.
A complete runnable program is good.
Trying to insert user to DB.
_, err = o.Insert(&user)
if err != nil {
beego.Error("UserController:Post - Got err inserting user to the database: ", err)
u.Ctx.Output.SetStatus(500)
return
}
What did you expect to see?
The user is inserted and no error occurred
What did you see instead?
I see the following error:
Got err inserting user to the database: no LastInsertId available
I also met this issue. My environment is the same with AvoncourtPartners.
My code is:
_, _, err := o.ReadOrCreate(u, "id")
If the data is new, data will be created, but got err message: no LastInsertId available.
if data is already in database, the server will crash with msg:
Handler crashed with error reflect: call of reflect.Value.Int on string Value
I think I know the reason why the program will crash. My Id field is a string rather than int.
What Database Driver and System are you using?
same issue with o.Insert(&user)
Same issue with PostgreSQL 10.4
Agree with AllenDang on the Id column should be the reason,and maybe this is the root cause from
pg driver
pq does not support the LastInsertId() method of the Result type in database/sql.
To return the identifier of an INSERT (or UPDATE or DELETE), use the Postgres
RETURNING clause with a standard Query or QueryRow call:
var userid int
err := db.QueryRow(`INSERT INTO users(name, favorite_fruit, age)
VALUES('beatrice', 'starfruit', 93) RETURNING id`).Scan(&userid)
For more details on RETURNING, see the Postgres documentation:
http://www.postgresql.org/docs/current/static/sql-insert.html
http://www.postgresql.org/docs/current/static/sql-update.html
http://www.postgresql.org/docs/current/static/sql-delete.html
Most helpful comment
[email protected]
same issue with
o.Insert(&user)