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

- transaction: exec: mismatched param and argument count - when trying to Update #1

Closed
aleblanc70 opened this issue Jul 5, 2023 · 3 comments
Labels
bug Something isn't working

Comments

@aleblanc70
Copy link

aleblanc70 commented Jul 5, 2023

Following your new article:
https://medium.com/@kataras/how-to-use-iris-and-postgresql-for-web-development-e8c46d72f1e6

I was able to use the pg middleware for testing. It seem I cannot do update!
The error I got: transaction: exec: mismatched param and argument count

type User struct {
	Id         string    `json:"id" pg:"type=uuid,primary"`
	CreatedAt  time.Time `json:"createdAt" pg:"type=timestamp,default=clock_timestamp()"`
	UpdatedAt  time.Time `json:"updatedAt" pg:"type=timestamp,default=clock_timestamp()"`
	Email      string    `json:"email" pg:"type=varchar(255),username,unique"`
	Password   string    `json:"password" pg:"type=varchar(32),password"`
	FirstName  string    `json:"firstName" pg:"type=varchar(255)"`
	MiddleName *string   `json:"middleName" pg:"type=varchar(255),null"`
	LastName   string    `json:"lastName" pg:"type=varchar(255)"`
	Sex        *string   `json:"sex" pg:"type=char(1),null"`
}
func (c UserController) Update(ctx iris.Context) {
	var u User
	var errPg error
	err := ctx.ReadJSON(&u)
	if err != nil {
		ctx.StopWithProblem(iris.StatusBadRequest, iris.NewProblem().
			Title("User Update failure").DetailErr(err))
		return
	}

	userRepo := pg.Repository[User](ctx)
	_, errPg = userRepo.Update(ctx, u)
	if errPg != nil {
		if pg.IsErrNoRows(errPg) {
			ctx.StopWithStatus(iris.StatusNotFound)
		} else {
			ctx.StopWithError(iris.StatusInternalServerError, errPg)
		}
		return
	}
	ctx.StatusCode(iris.StatusOK)
}

From Postman I Put:
{
"id": "6c051e51-9c64-49f7-a6ce-977cd7b2030a",
"email": "[email protected]",
"password": "Ironman",
"firstName": "Tony",
"middleName": "",
"lastName": "Stark",
"sex": "M"
},

Same error if I add my CreatedAt and UpdatedAt in my json

I tried to debug further, and it remove my audit dates because there are timestamp and put my id at the end not sure what it try to do.

Any help will be appreciated!

My project is here:
https://github.com/aleblanc70/events/blob/main/web/main.go
Thanks!

@aleblanc70
Copy link
Author

I tried with my event model

{
    "id": "a613ab5d-9cc1-45ca-ba1d-9bc8e4fcff2f",
    "title": "Ironman 2024",
    "description": "IronMan"
}

When I reach the code:

func (db *DB) updateTableRecord(ctx context.Context, value any, columnsToUpdate []string, primaryKey *desc.Column) (int64, error) {
	// build the SQL query and arguments using the table definition and its primary key.
	query, args, err := desc.BuildUpdateQuery(value, columnsToUpdate, primaryKey)
	if err != nil {
		return 0, err
	}

	// execute the query using db.Exec and pass in the primary key values as a parameter
	tag, err := db.Exec(ctx, query, args...)
	if err != nil {
		return 0, err
	}

	return tag.RowsAffected(), nil
}

My query have 4 arguments and my args got 3 element only. Look like the id is causing the issue maybe...

UPDATE "events" SET title = $1,description = $2,id = $3 WHERE "id" = $4;

Should I have Id twice in my args parameter ?

My project is here:
https://github.com/aleblanc70/events/blob/main/web/main.go

@kataras kataras added the bug Something isn't working label Jul 6, 2023
@kataras
Copy link
Owner

kataras commented Jul 6, 2023

@kataras kataras closed this as completed in 6f2375b Jul 6, 2023
@kataras
Copy link
Owner

kataras commented Jul 6, 2023

Fixed @aleblanc70, thanks a lot for the bug report! Don't hesitate to post more of those

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants