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

database/gdb: When (update/insert) operation, if the number of fields in the data table is 0, an error will be returned directly #3554

Open
wln32 opened this issue Apr 30, 2024 · 1 comment · May be fixed by #3553

Comments

@wln32
Copy link
Member

wln32 commented Apr 30, 2024

Description

type Data struct{
   Id int64
   IntCol int64 
}

createSql:=`CREATE TABLE IF NOT EXISTS %s (
id SERIAL PRIMARY KEY,
int_col INT);`
pgsql := g.DB("pgsql")
tableName := "Type_Table_" + gtime.Now().TimestampMilliStr()
// pgsql 会把表名转换为小写
pgsql.Exec(ctx, fmt.Sprintf(createSql, tableName))

// 插入数据
// 随便写几个数据
var data = Data {
    Id :1,
   IntCol :2,
}
// 这里的tableName用的时上面的,同一个变量
_, err := pgsql.Model(tableName).Data(data).Insert()
fmt.Println(err)
// output:
// INSERT INTO "Type_Table_1714453562918"("") VALUES() : pq: 长度为0的分隔标示符 在 """" 或附近的
// 给出的错误让人迷惑

// 表名同样时刚开始创建表的那个,在表里随便插入一条数据之后
// 更新数据,
_, err = pgsql.Model(tableName).Where("id", 1).Data(g.Map{
		"int_col": 9999,
}).Update()
fmt.Printf("%+v\n", err)
// output:
// data cannot be empty
// 给出的错误同样让人迷惑

经过调试后发现(c *Core) mappingAndFilterData 这个方法会获取表的所有字段,然后和要插入的结构体字段名做对比,删掉不存在的,但是由于我在Insert操作时传入的表名不是全小写的,所以查不到字段,也没有返回错误,导致后面在Insert时,没有数据,然后报错

// 插入时
3333

// 更新时
4444

Additional

No response

@Issues-translate-bot
Copy link

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


Description

type Data struct{
   ID int64
   IntCol int64
}

createSql:=`CREATE TABLE IF NOT EXISTS %s (
id SERIAL PRIMARY KEY,
int_col INT);`
pgsql := g.DB("pgsql")
tableName := "Type_Table_" + gtime.Now().TimestampMilliStr()
// pgsql will convert the table name to lowercase
pgsql.Exec(ctx, fmt.Sprintf(createSql, tableName))

//Insert data
//Write any data
var data = Data {
    Id:1,
   IntCol:2,
}
// The tableName here is the same variable as above.
_, err := pgsql.Model(tableName).Data(data).Insert()
fmt.Println(err)
//output:
// INSERT INTO "Type_Table_1714453562918"("") VALUES() : pq: delimiter identifier of length 0 at or near """"
// The error given is confusing

// When the table name is the same as the one that just created the table, after randomly inserting a piece of data into the table
// update data,
_, err = pgsql.Model(tableName).Where("id", 1).Data(g.Map{
"int_col": 9999,
}).Update()
fmt.Printf("%+v\n", err)
//output:
// data cannot be empty
// The error given is also confusing

After debugging, I found that (c *Core) mappingAndFilterData This method will get all the fields of the table, and then compare it with the field name of the structure to be inserted, and delete the ones that do not exist, but because the table I passed in during the Insert operation The name is not all lowercase, so the field cannot be found and no error is returned. As a result, when inserting later, there is no data and an error is reported.

// when inserting
3333

// when updating
4444

Additional

No response

@Issues-translate-bot Issues-translate-bot changed the title database/gdb: 当(更新/插入)操作时,如果数据表字段数量为0,直接返回错误 database/gdb: When (update/insert) operation, if the number of fields in the data table is 0, an error will be returned directly Apr 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants