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

When using the goctl model command to generate Go code for PostgreSQL, the auto-increment primary key cannot be null, and passing a 0 value does not trigger auto-incrementation. #4565

Open
SanJan-Loe opened this issue Jan 13, 2025 · 1 comment

Comments

@SanJan-Loe
Copy link

Describe the bug
当使用gotcl 工具,将pgsql sql文件转化为go的model代码时,的auto_increment特征导致,生成的值为int64,而不是sql.NULLINT64,调用默认生成的Insert函数时,传入的
To Reproduce
Steps to reproduce the behavior, if applicable:

  1. The code is

对应的sql代码:

 "registration_order"            BIGINT    UNIQUE    GENERATED BY DEFAULT AS IDENTITY

生成的go model代码
RegistrationOrder int64 db:"registration_order"

对应的Insert代码:
func (m *defaultUserModel) Insert(ctx context.Context, data *User) (sql.Result, error) {
publicUserAccountKey := fmt.Sprintf("%s%v", cachePublicUserAccountPrefix, data.Account)
publicUserEmailKey := fmt.Sprintf("%s%v", cachePublicUserEmailPrefix, data.Email)
publicUserExtendidKey := fmt.Sprintf("%s%v", cachePublicUserExtendidPrefix, data.Extendid)
publicUserGsucidKey := fmt.Sprintf("%s%v", cachePublicUserGsucidPrefix, data.Gsucid)
publicUserIdKey := fmt.Sprintf("%s%v", cachePublicUserIdPrefix, data.Id)
publicUserPhonenumberKey := fmt.Sprintf("%s%v", cachePublicUserPhonenumberPrefix, data.Phonenumber)
ret, err := m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
query := fmt.Sprintf("insert into %s (%s) values ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16)", m.table, userRowsExpectAutoSet)
return conn.ExecCtx(ctx, query, data.Id, data.Extendid, data.Gsucid, data.Name, data.Account, data.Password, data.Email, data.Phonenumber, data.Status, data.Errorlogincount, data.Refreshtoken, data.RegistrationOrder, data.Lastlogintime, data.Createtime, data.Updatetime, data.Deletetime)
}, publicUserAccountKey, publicUserEmailKey, publicUserExtendidKey, publicUserGsucidKey, publicUserIdKey, publicUserPhonenumberKey)
return ret, err
}

  1. The error is

默认重复插入registration_order为0的数据时,出现如下错误:
error in inserting user:pq: duplicate key value violates unique constraint "user_registration_order_key"

@kesonan
Copy link
Collaborator

kesonan commented Jan 15, 2025

  1. If you want sql.NULLINT64 instead of int64, you should see the document and try it again.
  2. The error log maybe cause by your constraint incorrect, pls take a check, it's not cause by goctl.

@kevwan kevwan changed the title goctl model指令生成pgsql 的go代码时自增主键不能为空,传0值无法自增 When using the goctl model command to generate Go code for PostgreSQL, the auto-increment primary key cannot be null, and passing a 0 value does not trigger auto-incrementation. Jan 17, 2025
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

No branches or pull requests

2 participants