diff --git a/contrib/drivers/mysql/mysql_z_unit_model_test.go b/contrib/drivers/mysql/mysql_z_unit_model_test.go index 5ba6f9e2408..f1a8b604675 100644 --- a/contrib/drivers/mysql/mysql_z_unit_model_test.go +++ b/contrib/drivers/mysql/mysql_z_unit_model_test.go @@ -4788,21 +4788,21 @@ func Test_Model_FixGdbJoin(t *testing.T) { }) } -func TestOrderByStatementGenerated(t *testing.T) { +func Test_OrderBy_Statement_Generated(t *testing.T) { gtest.C(t, func(t *gtest.T) { - array := gstr.SplitAndTrim(gtest.DataContent(`table_with_prefix.sql`), ";") + array := gstr.SplitAndTrim(gtest.DataContent(`fix_gdb_order_by.sql`), ";") for _, v := range array { if _, err := db.Exec(ctx, v); err != nil { gtest.Error(err) } } - defer dropTable(`instance`) + defer dropTable(`employee`) sqlArray, _ := gdb.CatchSQL(ctx, func(ctx context.Context) error { - g.DB("default").Ctx(ctx).Model("instance").Order("f_id asc", "name desc").All() + g.DB("default").Ctx(ctx).Model("employee").Order("name asc", "age desc").All() return nil }) rawSql := strings.ReplaceAll(sqlArray[len(sqlArray)-1], " ", "") - expectSql := strings.ReplaceAll("SELECT * FROM `instance` ORDER BY `f_id` asc, `name` desc", " ", "") + expectSql := strings.ReplaceAll("SELECT * FROM `employee` ORDER BY `name` asc, `age` desc", " ", "") t.Assert(rawSql, expectSql) }) } diff --git a/contrib/drivers/mysql/testdata/fix_gdb_order_by.sql b/contrib/drivers/mysql/testdata/fix_gdb_order_by.sql new file mode 100644 index 00000000000..2ba712641fa --- /dev/null +++ b/contrib/drivers/mysql/testdata/fix_gdb_order_by.sql @@ -0,0 +1,9 @@ +CREATE TABLE IF NOT EXISTS `employee` +( + id BIGINT AUTO_INCREMENT PRIMARY KEY, + name VARCHAR(255) NOT NULL, + age INT NOT NULL +); + +INSERT INTO employee(name, age) VALUES ('John', 30); +INSERT INTO employee(name, age) VALUES ('Mary', 28); \ No newline at end of file