go-zero icon indicating copy to clipboard operation
go-zero copied to clipboard

gen model with field `create_at` causes field count not match

Open sdxstar opened this issue 2 years ago • 4 comments

use ddl gen model with field create_at causes field count not match A clear and concise description of what the bug is.

To Reproduce Steps to reproduce the behavior, if applicable:

  1. The sql is
CREATE TABLE `login_history`
(
    `id`         bigint      NOT NULL AUTO_INCREMENT,
    `user_id`    bigint      NOT NULL DEFAULT 0 COMMENT '用户ID',
    `login_ip`   varchar(20) NOT NULL DEFAULT '' COMMENT '登录IP',
    `login_time` timestamp   NOT NULL COMMENT '登录时间',
    `create_at`  timestamp   NULL     DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    `update_at`  timestamp   NULL     DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    PRIMARY KEY (`id`)
) ENGINE = InnoDB
  AUTO_INCREMENT = 1
  DEFAULT CHARSET = utf8mb4;

generated codes:

	loginHistoryRowsExpectAutoSet   = strings.Join(stringx.Remove(loginHistoryFieldNames, "`id`", "`create_time`", "`update_time`", "`create_at`", "`update_at`"), ",")
	loginHistoryRowsWithPlaceHolder = strings.Join(stringx.Remove(loginHistoryFieldNames, "`id`", "`create_time`", "`update_time`", "`create_at`", "`update_at`"), "=?,") + "=?"

func (m *defaultLoginHistoryModel) Insert(ctx context.Context, data *LoginHistory) (sql.Result, error) {
	loginHistoryIdKey := fmt.Sprintf("%s%v", cacheLoginHistoryIdPrefix, data.Id)
	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 (?, ?, ?, ?, ?)", m.table, loginHistoryRowsExpectAutoSet)
		return conn.ExecCtx(ctx, query, data.UserId, data.LoginIp, data.LoginTime, data.CreateAt, data.UpdateAt)
	}, loginHistoryIdKey)
	return ret, err
}

func (m *defaultLoginHistoryModel) Update(ctx context.Context, data *LoginHistory) error {
	loginHistoryIdKey := fmt.Sprintf("%s%v", cacheLoginHistoryIdPrefix, data.Id)
	_, err := m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
		query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, loginHistoryRowsWithPlaceHolder)
		return conn.ExecCtx(ctx, query, data.UserId, data.LoginIp, data.LoginTime, data.CreateAt, data.UpdateAt, data.Id)
	}, loginHistoryIdKey)
	return err
}
  1. The error is

    Error 1136: Column count doesn't match value count at row 1
    

More description Add any other context about the problem here.

sdxstar avatar Jun 08 '22 06:06 sdxstar

env: go-ctl version 1.3.8

sdxstar avatar Jun 08 '22 06:06 sdxstar

error log: 2022-06-08 15:32:09.781 ERROR sqlx/utils.go:139 stmt: insert into login_history (user_id,login_ip,login_time) values (1, '::1', 2022-06-08 15:32:09 +0800 CST, 0001-01-01 00:00:00 +0000 UTC, 0001-01-01 00:00:00 +0000 UTC), error: Error 1136: Column count doesn't match value count at row 1 {"trace": "0480f2901d6bd1ba416945661d914189", "span": "43a7a4e74556981d"}

update method has the same bug

sdxstar avatar Jun 08 '22 07:06 sdxstar

It's a pity that is a known issue,I am considering to use sql builder to solve it on next release.

kesonan avatar Jun 29 '22 14:06 kesonan

This issue is stale because it has been open for 30 days with no activity.

github-actions[bot] avatar Jul 31 '22 02:07 github-actions[bot]

This issue was closed because it has been inactive for 14 days since being marked as stale.

github-actions[bot] avatar Aug 14 '22 02:08 github-actions[bot]