From 6160c8c670d131237128946e3fd7e41474ab1bc8 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Tue, 3 Mar 2020 12:45:27 +0000 Subject: [PATCH] fix bug on deleted with join (#1570) fix bug on deleted with join Reviewed-on: https://gitea.com/xorm/xorm/pulls/1570 --- internal/statements/statement.go | 11 +++++++--- session_find_test.go | 35 ++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/internal/statements/statement.go b/internal/statements/statement.go index 68738b90..90771d4b 100644 --- a/internal/statements/statement.go +++ b/internal/statements/statement.go @@ -992,18 +992,23 @@ func (statement *Statement) joinColumns(cols []*schemas.Column, includeTableName // CondDeleted returns the conditions whether a record is soft deleted. func (statement *Statement) CondDeleted(col *schemas.Column) builder.Cond { + var colName = col.Name + if statement.JoinStr != "" { + colName = statement.quote(statement.TableName()) + + "." + statement.quote(col.Name) + } var cond = builder.NewCond() if col.SQLType.IsNumeric() { - cond = builder.Eq{col.Name: 0} + cond = builder.Eq{colName: 0} } else { // FIXME: mssql: The conversion of a nvarchar data type to a datetime data type resulted in an out-of-range value. if statement.dialect.DBType() != schemas.MSSQL { - cond = builder.Eq{col.Name: utils.ZeroTime1} + cond = builder.Eq{colName: utils.ZeroTime1} } } if col.Nullable { - cond = cond.Or(builder.IsNull{col.Name}) + cond = cond.Or(builder.IsNull{colName}) } return cond diff --git a/session_find_test.go b/session_find_test.go index ad9d1668..4345fcf6 100644 --- a/session_find_test.go +++ b/session_find_test.go @@ -833,3 +833,38 @@ func TestJoinFindLimit(t *testing.T) { Limit(10, 10).Find(&finds) assert.NoError(t, err) } + +func TestMoreExtends(t *testing.T) { + type MoreExtendsUsers struct { + ID int64 `xorm:"id autoincr pk" json:"id"` + Name string `xorm:"name not null" json:"name"` + CreatedAt time.Time `xorm:"created not null" json:"created_at"` + UpdatedAt time.Time `xorm:"updated not null" json:"updated_at"` + DeletedAt time.Time `xorm:"deleted" json:"deleted_at"` + } + + type MoreExtendsBooks struct { + ID int64 `xorm:"id autoincr pk" json:"id"` + Name string `xorm:"name not null" json:"name"` + UserID int64 `xorm:"user_id not null" json:"user_id"` + CreatedAt time.Time `xorm:"created not null" json:"created_at"` + UpdatedAt time.Time `xorm:"updated not null" json:"updated_at"` + DeletedAt time.Time `xorm:"deleted" json:"deleted_at"` + } + + type MoreExtendsBooksExtend struct { + MoreExtendsBooks `xorm:"extends"` + Users MoreExtendsUsers `xorm:"extends" json:"users"` + } + + assert.NoError(t, prepareEngine()) + assertSync(t, new(MoreExtendsUsers), new(MoreExtendsBooks)) + + var books []MoreExtendsBooksExtend + err := testEngine.Table("more_extends_books").Select("more_extends_books.*, more_extends_users.*"). + Join("INNER", "more_extends_users", "more_extends_books.user_id = more_extends_users.id"). + Where("more_extends_books.name LIKE ?", "abc"). + Limit(10, 10). + Find(&books) + assert.NoError(t, err) +}