go-xorm / xorm

Simple and Powerful ORM for Go, support mysql,postgres,tidb,sqlite3,mssql,oracle, Moved to https://gitea.com/xorm/xorm
BSD 3-Clause "New" or "Revised" License
6.66k stars 754 forks source link

Insert doesn't work if struct has `xorm:"extends"` #1049

Open sergey-koumirov opened 6 years ago

sergey-koumirov commented 6 years ago
package main

import(
  "fmt"
  "github.com/go-xorm/xorm"
  _ "github.com/go-sql-driver/mysql"
)

type Users struct {
    Id int64 `xorm:"pk"`
    Name string `xorm:"name"`
}

type Mscans struct {
    Id int64 `xorm:"pk"`
    UserId int64 `xorm:"user_id"`
    Owner Users `xorm:"extends"`
}

func main() {

  engine, _ := xorm.NewEngine("mysql", "")
  engine.ShowSQL(true)

  m := Mscans{UserId: 34}
  _, err := engine.Insert(&m)
  fmt.Println(err)

}
go run test.go
[xorm] [info]  2018/07/24 18:25:27.693703 [SQL] INSERT INTO `mscans` (`id`,`user_id`,`id`,`name`) VALUES (?, ?, ?, ?) []interface {}{0, 34, 0, ""}
Error 1110: Column 'id' specified twice

I just want to create new record in mscans table. User with id=34 already exists.

lunny commented 5 years ago

It's difficult when you have two columns have same names.