gobuffalo / pop

A Tasty Treat For All Your Database Needs
MIT License
1.45k stars 243 forks source link

has_many_association bug #470

Open hookover opened 5 years ago

hookover commented 5 years ago

debug

runtime/debug.Stack(0x502c9dd, 0x2, 0xc000318838)
        /usr/local/Cellar/go/1.13.4/libexec/src/runtime/debug/stack.go:24 +0x9d
git.liebaopay.com/INA_financial/rpc_services/app/srv/workflow/services.(*Workflow).CreateNode.func1(0xc0002e12c0)
        /app/go/rpc_services/app/srv/workflow/services/node.go:30 +0xde
panic(0x4e53060, 0xc000400120)
        /usr/local/Cellar/go/1.13.4/libexec/src/runtime/panic.go:679 +0x1b2
reflect.valueInterface(0x0, 0x0, 0x0, 0x502ca01, 0x2, 0x0)
        /usr/local/Cellar/go/1.13.4/libexec/src/reflect/value.go:1008 +0x1a0
reflect.Value.Interface(...)
        /usr/local/Cellar/go/1.13.4/libexec/src/reflect/value.go:1003
github.com/gobuffalo/pop/associations.(*hasManyAssociation).AfterProcess(0xc0001460f0, 0x0, 0x0, 0x0, 0x0, 0x0)
        /Users/chenjiang/go/pkg/mod/github.com/gobuffalo/pop@v4.12.2+incompatible/associations/has_many_association.go:133 +0x16c
github.com/gobuffalo/pop.(*Connection).Create.func1.1(0xbf6f0b7565401648, 0x1fecb0180)
        /Users/chenjiang/go/pkg/mod/github.com/gobuffalo/pop@v4.12.2+incompatible/executors.go:273 +0x36d

incompatible/associations/has_many_association.go:133 +0x16c

func (a *hasManyAssociation) AfterProcess() AssociationStatement {
    v := a.value
    if v.Kind() == reflect.Ptr {
        v = v.Elem()
    }

    belongingIDFieldName := "ID"

    ownerIDFieldName := "ID"
    ownerID := 
       reflect.Indirect(reflect.ValueOf(a.owner)).FieldByName(ownerIDFieldName).Interface()  <--

model

type WorkflowNode struct {
    Id         int64          `json:"id" db:"id"`   <----- not ID
    WorkflowId int64          `json:"workflow_id" db:"workflow_id"`
    Name       string         `json:"name" db:"name"`
    Reviewers  types.JSONText `json:"reviewers" db:"reviewers" `
    Data       string         `json:"data" db:"data" `
    Rules      types.JSONText `json:"rules" db:"rules" `
    CreatedAt  int64          `json:"created_at" db:"created_at"`
    UpdatedAt  int64          `json:"updated_at" db:"updated_at"`

        //This line of code causes bug
    FromNodeEdges []WorkflowEdges `has_many:"workflow_graphs" primary_key:"id" fk_id:"from_node_id"`
}

run

    node := models.WorkflowNode{
        Name:       in.Name,
        WorkflowId: in.WorkflowId,
        Data:       in.Data,
        Reviewers:  types.JSONText(string(reviewersJsonByte)),
        Rules:      types.JSONText(in.Rules),
    }

    err = p.Def().Create(&node)
    if err != nil {
        out.Code = proto.Code_fail
        out.Message = MessageSystemException
        return nil
    }
dmuriel commented 5 years ago

Hello, I think mine is related too because I have the same error when trying to create (not eager create) a user that contains "has_many" association.

So every time I want to create something I have to allocate the associated as empty like:

user := &models.User{}

if err := c.Bind(user); err != nil {
    return c.Render(http.StatusBadRequest, r.HTML("auth/signup.html"))
}

user.Portals = &models.UserCompanies{} "<--- Without this I get the error @hookover shows"

if err := tx.Create(user); err != nil {
    return err
}

Is that the correct behavior?, I thought that when we want to create something with associations we should use "eager().Create(&model)"

hookover commented 4 years ago

The ID field of this framework must be completely uppercase, otherwise problems will occur, including when querying a single record, the value of the ID field will be 0. like this:

type User struct {
Id int `db:"id"` //auto_increment
Name string `db:"name"`
}

u := &User{Name:"test"}
err :=pop.Create(u); 
err == nil
//fmt.print(u.Id, u.Name)   // 0, test  ,the id in database is a 1 or more