-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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: postgresql Composite primary key for non-ID primary key LastInsertId() support #3675
Comments
DescriptionScenes:
Example: _, err := dao.Company.
OmitEmpty().
OnConflict(
dao.Company.Columns().Name,
dao.Company.Columns().ProjectId,
).
OnDuplicateEx(
dao.Company.Columns().Id,
dao.Company.Columns().Name,
dao.Company.Columns().ProjectId,
dao.Company.Columns().CreateTime,
).Save(companies) need:
question:
Temporary solution: As you can see from the following code, you can control the fields returned by the RETURNING statement by configuring the internalPrimaryKeyInCtx value in ctx
const (
internalPrimaryKeyInCtx gctx.StrKey = "primary_key"
)
// Check if it is an insert operation with primary key.
if value := ctx.Value(internalPrimaryKeyInCtx); value != nil {
var OK bool
pkField, ok = value.(gdb.TableField)
if !ok {
isUseCoreDoExec = true
}
} else {
isUseCoreDoExec = true
}
// check if it is an insert operation.
if !isUseCoreDoExec && pkField.Name != "" && strings.Contains(sql, "INSERT INTO") {
primaryKey = pkField.Name
sql += fmt.Sprintf(` RETURNING "%s"`, primaryKey)
} else {
// use default DoExec
return d.Core.DoExec(ctx, link, sql, args...)
} The following code shows that the LastInsertId() operation requires RETURNING that the fields in xxx need to be of type int
Therefore, after changing the code to the following, LastInsertId() can be called normally:
Summarize:
AdditionalNo response |
Description
场景:
Example:
需求:
问题:
INSERT INTO ... RETURNING "name"
,在执行 LastInsertId() 时将返回LastInsertId is not supported by primary key type
错误临时解决方案:
从如下代码中可以看到,可以通过在 ctx 中配置 internalPrimaryKeyInCtx 值来控制 RETURNING 语句返回的字段
如下代码展示了 LastInsertId() 操作需要 RETURNING xxx 中的字段需要为 int 类型
因此修改为如下代码后可正常调用 LastInsertId():
总结:
Additional
No response
The text was updated successfully, but these errors were encountered: