Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the bug that ScanAndCount and AllAndCount report errors in sqlserver #3155

Merged
merged 4 commits into from
Nov 16, 2023

Conversation

wlqe
Copy link
Contributor

@wlqe wlqe commented Nov 14, 2023

AllAndCount and ScanAndCount throw errors in SQL Server environment when there is an orderby statement。

sql server 2022 statement:
drop table if exists demo
create table demo(recid int identity(1,1),txt varchar(100),primary key(recid))
insert into demo(txt)values('txt1'),('txt2'),('txt3')
select * from demo

example code:
import (
"fmt"
_ "github.com/gogf/gf/contrib/drivers/mssql/v2"
"github.com/gogf/gf/v2/database/gdb"
)

  func main() {
      db, err := gdb.New(gdb.ConfigNode{
	      Link: `mssql:sa:123456@tcp(192.168.1.200:1433)/demo`,
      })
      if err != nil {
	      panic(err)
      }
  
      count, i, err := db.Model("demo").Order("recid").Limit(0, 10).AllAndCount(false)
      if err != nil {
	      fmt.Println(err)
	      return
      }
      fmt.Println(count, i)
  }

Error message:
SELECT COUNT(1) FROM demo ORDER BY recid: mssql: Column "demo.recid" is invalid in the ORDER BY clause because it is not contained in either an aggregate function or the GROUP By clause.

Copy link
Member

@gqcn gqcn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wlqe Please add associated unit testing case for this update in crontrib/drivers/mssql: https://github.com/gogf/gf/blob/master/contrib/drivers/mssql/mssql_z_model_test.go

@wlqe
Copy link
Contributor Author

wlqe commented Nov 14, 2023

contrib/drivers/mssql/mssql_z_model_test.go新加的单元测试代码如下:(不知道怎么把新变动的文件加到这个pr中来,只好直接贴在这了,哪位老大辛苦把代码加到mssql_z_model_test.go文件中吧)

func Test_Model_AllAndCount(t *testing.T) {
table := createInitTable()
defer dropTable(table)

gtest.C(t, func(t *gtest.T) {
	result, total, err := db.Model(table).Order("id").Limit(0, 3).AllAndCount(false)
	t.Assert(err, nil)

	t.Assert(len(result), 3)
	t.Assert(total, TableSize)
})

}

func Test_Model_ScanAndCount(t *testing.T) {
table := createInitTable()
defer dropTable(table)

gtest.C(t, func(t *gtest.T) {
	type User struct {
		Id         int
		Passport   string
		Password   string
		NickName   string
		CreateTime gtime.Time
	}

	users := make([]User, 0)
	total := 0

	err := db.Model(table).Order("id").Limit(0, 3).ScanAndCount(&users, &total, false)
	t.Assert(err, nil)

	t.Assert(len(users), 3)
	t.Assert(total, TableSize)
})

}

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


The newly added unit test code of contrib/drivers/mssql/mssql_z_model_test.go is as follows: (I don’t know how to add the newly changed files to this PR, so I have to paste it directly here. Who has worked hard to add the code to mssql_z_model_test. go file)

func Test_Model_AllAndCount(t *testing.T) {
table := createInitTable()
defer dropTable(table)

gtest.C(t, func(t *gtest.T) {
result, total, err := db.Model(table).Order("id").Limit(0, 3).AllAndCount(false)
t.Assert(err, nil)

t.Assert(len(result), 3)
t.Assert(total, TableSize)
})
}

func Test_Model_ScanAndCount(t *testing.T) {
table := createInitTable()
defer dropTable(table)

gtest.C(t, func(t *gtest.T) {
typeUser struct {
ID int
Passport string
Password string
NickName string
CreateTime gtime.Time
}

users := make([]User, 0)
total := 0

err := db.Model(table).Order("id").Limit(0, 3).ScanAndCount(&users, &total, false)
t.Assert(err, nil)

t.Assert(len(users), 3)
t.Assert(total, TableSize)
})
}

@zcyc
Copy link
Contributor

zcyc commented Nov 15, 2023

contrib/drivers/mssql/mssql_z_model_test.go新加的单元测试代码如下:(不知道怎么把新变动的文件加到这个pr中来,只好直接贴在这了,哪位老大辛苦把代码加到mssql_z_model_test.go文件中吧)

func Test_Model_AllAndCount(t *testing.T) { table := createInitTable() defer dropTable(table)

gtest.C(t, func(t *gtest.T) {
	result, total, err := db.Model(table).Order("id").Limit(0, 3).AllAndCount(false)
	t.Assert(err, nil)

	t.Assert(len(result), 3)
	t.Assert(total, TableSize)
})

}

func Test_Model_ScanAndCount(t *testing.T) { table := createInitTable() defer dropTable(table)

gtest.C(t, func(t *gtest.T) {
	type User struct {
		Id         int
		Passport   string
		Password   string
		NickName   string
		CreateTime gtime.Time
	}

	users := make([]User, 0)
	total := 0

	err := db.Model(table).Order("id").Limit(0, 3).ScanAndCount(&users, &total, false)
	t.Assert(err, nil)

	t.Assert(len(users), 3)
	t.Assert(total, TableSize)
})

}

在你pr的分支继续commit就会自动加到pr中。

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


The newly added unit test code of contrib/drivers/mssql/mssql_z_model_test.go is as follows: (I don’t know how to add the newly changed files to this PR, so I have to paste it directly here. Who has worked hard to add the code to mssql_z_model_test .go file)

func Test_Model_AllAndCount(t *testing.T) { table := createInitTable() defer dropTable(table)

gtest.C(t, func(t *gtest.T) {
result, total, err := db.Model(table).Order("id").Limit(0, 3).AllAndCount(false)
t.Assert(err, nil)

t.Assert(len(result), 3)
t.Assert(total, TableSize)
})

}

func Test_Model_ScanAndCount(t *testing.T) { table := createInitTable() defer dropTable(table)

gtest.C(t, func(t *gtest.T) {
type User struct {
Id int
Passport string
Password string
NickName string
CreateTime gtime.Time
}

users := make([]User, 0)
total := 0

err := db.Model(table).Order("id").Limit(0, 3).ScanAndCount(&users, &total, false)
t.Assert(err, nil)

t.Assert(len(users), 3)
t.Assert(total, TableSize)
})

}

If you continue to commit on the branch of your pr, it will be automatically added to the pr.

@wlqe
Copy link
Contributor Author

wlqe commented Nov 15, 2023

@zcyc thanks. Resubmitted.

@gqcn gqcn merged commit 85acd3d into gogf:master Nov 16, 2023
20 checks passed
zhangyuyu pushed a commit to zhangyuyu/gf that referenced this pull request Dec 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants