jackx PGX scan error - converting driver.Value type time.Time to a int: invalid syntax Golang Postgres

161 Views Asked by At

I get a scan error after inserting a row into the database. Below is my database structure.

CREATE TABLE IF NOT EXISTS "users" (
  "id" bigserial PRIMARY KEY,
  "email" varchar UNIQUE NOT NULL,
  "first_name" varchar NOT NULL,
  "last_name" varchar NOT NULL,
  "hashad_password" varchar NOT NULL,
  "active" bigint NOT NULL,
  "roll_id" bigint NOT NULL DEFAULT 3,
  "created_at" timestamptz NOT NULL DEFAULT (now()),
  "updated_at" timestamptz NOT NULL DEFAULT (now()),
  "password_changed_at" timestamptz NOT NULL DEFAULT (now())
);

And Below is Golang code.

var newID int
    stmt := `INSERT INTO users (email, first_name, last_name, hashad_password, active, roll_id) values ($1, $2, $3, $4, $5, $6) returning id`

    err = db.QueryRow(stmt,
        user.Email,
        user.FirstName,
        user.LastName,
        hashedPassword,
        user.Active,
        user.RollId,
    ).Scan(&newID)

    if err != nil {
        return 0, err
    }

But every time I get a blow scan error.

sql: Scan error on column index 7, name "password_changed_at": converting driver.Value type time.Time ("2023-11-13 06:06:52.223458 +0000 UTC") to a int: invalid syntax

The row is inserted into the database successfully.

Thanks All

0

There are 0 best solutions below