空指针crash #1853

Closed
opened 3 months ago by junay · 2 comments
junay commented 3 months ago

应用场景:net/http + xorm
代码片段:


type Project struct {
	ID      int64     `json:"id"      xorm:"notnull pk unique autoincr 'id'" `
	UserID  int64     `json:"-"           xorm:"notnull BITINT 'uid'"`
	Name    string    `json:"name"    xorm:"notnull unique varchar(256) 'name'"`
	Config  string    `json:"config"  xorm:"notnull varchar(4096) 'config'"`
	Status  int       `json:"status"  xorm:"notnull INT 'status'"`
	Created time.Time `json:"created" xorm:"notnull created 'created'"`
	Updated time.Time `json:"updated" xorm:"notnull updated 'updated'"`
}

func (session *DBSession) GetAliveProject() ([]Project, error) {
	projects := []Project{}
	err := session.Session.Where("status = ?", types.PROJECT_STATUS_ALIVE).Find(&projects)
	if err != nil {
		jww.ERROR.Printf("fetch alive project failed: %s\n", err.Error())
		errRollback := session.Session.Rollback()
		if errRollback != nil {
			jww.ERROR.Printf(errRollback.Error())
		}
		return nil, err
	}

	return projects, nil
}

问题:
1、字段没有找到,但不管是数据库里还是结构体里都有这个status变量

ERROR 2021/01/17 22:36:40 fetch alive project failed: pq: column "status" does not exist

2、空指针crash

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0xa8 pc=0xeabe98]

goroutine 83 [running]:
xorm.io/xorm/schemas.(*Table).DeletedColumn(...)
        /datas/go_dev/src/xorm.io/xorm/schemas/table.go:111
xorm.io/xorm.(*Session).find(0xc0005280d0, 0x1392f00, 0xc0008185a0, 0x0, 0x0, 0x0, 0x0, 0x0)
        /datas/go_dev/src/xorm.io/xorm/session_find.go:126 +0x1d8
xorm.io/xorm.(*Session).Find(0xc0005280d0, 0x1392f00, 0xc0008185a0, 0x0, 0x0, 0x0, 0x0, 0x0)
        /datas/go_dev/src/xorm.io/xorm/session_find.go:31 +0xa4
web/services/web-server/db.(*DBSession).GetAliveProject(0xc000518018, 0x2572580, 0x0, 0x0, 0x0, 0x0)
        /datas/go_dev/src/web/services/web-server/db/project.go:58 +0xf5

应用场景:net/http + xorm 代码片段: ```go type Project struct { ID int64 `json:"id" xorm:"notnull pk unique autoincr 'id'" ` UserID int64 `json:"-" xorm:"notnull BITINT 'uid'"` Name string `json:"name" xorm:"notnull unique varchar(256) 'name'"` Config string `json:"config" xorm:"notnull varchar(4096) 'config'"` Status int `json:"status" xorm:"notnull INT 'status'"` Created time.Time `json:"created" xorm:"notnull created 'created'"` Updated time.Time `json:"updated" xorm:"notnull updated 'updated'"` } func (session *DBSession) GetAliveProject() ([]Project, error) { projects := []Project{} err := session.Session.Where("status = ?", types.PROJECT_STATUS_ALIVE).Find(&projects) if err != nil { jww.ERROR.Printf("fetch alive project failed: %s\n", err.Error()) errRollback := session.Session.Rollback() if errRollback != nil { jww.ERROR.Printf(errRollback.Error()) } return nil, err } return projects, nil } ``` 问题: 1、字段没有找到,但不管是数据库里还是结构体里都有这个`status`变量 ``` ERROR 2021/01/17 22:36:40 fetch alive project failed: pq: column "status" does not exist ``` 2、空指针crash ``` panic: runtime error: invalid memory address or nil pointer dereference [signal SIGSEGV: segmentation violation code=0x1 addr=0xa8 pc=0xeabe98] goroutine 83 [running]: xorm.io/xorm/schemas.(*Table).DeletedColumn(...) /datas/go_dev/src/xorm.io/xorm/schemas/table.go:111 xorm.io/xorm.(*Session).find(0xc0005280d0, 0x1392f00, 0xc0008185a0, 0x0, 0x0, 0x0, 0x0, 0x0) /datas/go_dev/src/xorm.io/xorm/session_find.go:126 +0x1d8 xorm.io/xorm.(*Session).Find(0xc0005280d0, 0x1392f00, 0xc0008185a0, 0x0, 0x0, 0x0, 0x0, 0x0) /datas/go_dev/src/xorm.io/xorm/session_find.go:31 +0xa4 web/services/web-server/db.(*DBSession).GetAliveProject(0xc000518018, 0x2572580, 0x0, 0x0, 0x0, 0x0) /datas/go_dev/src/web/services/web-server/db/project.go:58 +0xf5 ```
Owner
  1. Use "status" but status on your SQL texts
1. Use "status" but status on your SQL texts
Owner

use err := session.Session.Where("status = ?", types.PROJECT_STATUS_ALIVE).Find(&projects)

Because status is a reverse word of postgres.

use `err := session.Session.Where("`status` = ?", types.PROJECT_STATUS_ALIVE).Find(&projects)` Because status is a reverse word of postgres.
lunny closed this issue 4 weeks ago
Sign in to join this conversation.
No Milestone
No Assignees
2 Participants
Notifications
Due Date

No due date set.

Dependencies

This issue currently doesn't have any dependencies.

Loading…
There is no content yet.