🤷‍♀️ 🤷‍♂️ PostgreSQL error with using over jackc/pgx driver in Golang: cannot convert X to Y
Tags: postgres, go, beginners, showdev
Published: 2021-08-10 10:54:06

## Introduction

Hey, DEV people! 👋 Today, I am starting a new series of short articles called "**Simple Errors**", where I will cover various dead ends and pitfalls that I have encountered so far in my programming practice.

I will always try to follow four rules when writing such articles:

1. An explanation of the cause of the error;
2. A description of the input data in which the error occurred to me;
3. My solution to the error;
4. Conclusions and words of motivation;

Don't judge too harshly, write what you think of such articles, suggest your own topics for parsing in the comments... Here we go! 🚀

<a name="toc"></a>
### 📝 Table of contents

- [Explanation of the error](#explanation-of-the-error)
- [Input data when an error occurs](#input-data-when-an-error-occurs)
- [Resolving the error](#resolving-the-error)
- [Conclusions](#conclusions)

<a name="#explanation-of-the-error"></a>
## Explanation of the error 

When developing a REST API (or any other product) in Golang with PostgreSQL database over the high-performance driver [**jackc/pgx**](https://github.com/jackc/pgx), sometimes you may get a simple error that can be confusing to search for information. Especially, newcomers.

This simple error sounds like this: `cannot convert [SOMETHING] to [SOMETHING]`. In my case, it looked like `cannot convert 1 to Text`.

This error message comes from the **jackc/pgx** package and is caused by the combination of this package internally using prepared statements and PostgreSQL not being able to determine the type of the placeholders.

> 👍 Thanks for this explanation to author of this [comment](https://github.com/jackc/pgx/issues/798#issuecomment-665060509).

[↑ Table of contents](#toc)

<a name="#input-data-when-an-error-occurs"></a>
## Input data when an error occurs

First, let's take a look at the migration file that creates the projects table in my application's database:

```sql
-- ./platform/migration/000001_create_init_tables.up.sql

-- Create projects table
CREATE TABLE "projects" (
  "id" UUID PRIMARY KEY DEFAULT (uuid_generate_v4()),
  "created_at" timestamp DEFAULT (now()),
  "updated_at" timestamp,
  "user_id" UUID NOT NULL,
  "alias" varchar(255) UNIQUE NOT NULL,
  "project_status" int NOT NULL,
  "project_attrs" JSONB NOT NULL
);

// ...
```

We see that the fields have a very specific type, which will be validated by Postgres when creating/modifying data in this table.

Now, let's look at the Go model, which will fit this migration:

```go
// ./app/controllers/project_model.go

// Project struct to describe project object.
type Project struct {
	ID            uuid.UUID    `db:"id" json:"id"`
	CreatedAt     time.Time    `db:"created_at" json:"created_at"`
	UpdatedAt     time.Time    `db:"updated_at" json:"updated_at"`
	UserID        uuid.UUID    `db:"user_id" json:"user_id"`
	Alias         string       `db:"alias" json:"alias"`
	ProjectStatus int          `db:"project_status" json:"project_status"`
	ProjectAttrs  ProjectAttrs `db:"project_attrs" json:"project_attrs"`
}

// ProjectAttrs struct to describe project attributes.
type ProjectAttrs struct {
	Title       string `json:"title"`
	Description string `json:"description"`
	Picture     string `json:"picture"`
	URL         string `json:"url"`
}

// ...
```

Yes, you're quite right that the `JSOB` field type in the Go model has become a regular structure and will be stored in JSON format in the `project_attrs` database field.

> 👌 Other fields are quite normal for any Go project.

[↑ Table of contents](#toc)

![solve the problem](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gf7chbq35mtc2zgxk7zn.jpg)

<a name="#resolving-the-error"></a>
## Resolving the error

Let's move smoothly to the solution to this simple error.

All you need to know for the solution is that in PostgreSQL, you can specify types for placeholders directly in the query. Just simply add the types you specified in the migration file to the query.

The format for specifying the field type will be as follows: `$N::<TYPE>` (dollar sign + placeholder number + two colons + field type from DB).

```go
// ./app/controllers/project_query.go

// CreateProject method for creating project by given Project object.
func (q *ProjectQueries) CreateProject(p *models.Project) error {
	// Define query string.
	// We define type for each field to solve this simple error.
	query := `
	INSERT INTO projects 
	VALUES ($1::uuid, $2::timestamp, $3::timestamp, $4::uuid, $5::varchar, $6::int, $7::jsonb)
	`

	// Send query to database.
	_, err := q.Exec(query, p.ID, p.CreatedAt, p.UpdatedAt, p.UserID, p.Alias, p.ProjectStatus, p.ProjectAttrs)
	if err != nil {
		// Return only error.
		return err
	}

	// This query returns nothing.
	return nil
}

// ...
```

> ☝️ Once again, please note that we are specifying the PostgreSQL field type from the migration, not the Go structure from the model!

After building the project and requesting this endpoint, the error will no longer be displayed, and the request will succeed! 🎉

[↑ Table of contents](#toc)

<a name="#conclusions"></a>
## Conclusions

Personally, I like this kind of entry more than completely disabling this check in the **jackc/pgx** package config (more information [here](https://pkg.go.dev/github.com/jackc/pgx/v4#ConnConfig)).

Just because it gives you an immediate understanding of the field type at a glance of the SQL query, without having to dive into the details of the implementation in the controller.

Choose the option that is convenient to you and use it, I just showed the way of the solution, which helped me personally. Have a successful work and let simple errors **never stop you** on the way to realizing your projects! 😉

[↑ Table of contents](#toc)

<a name=""></a>
## Photos and videos by

- Arun Prakash https://unsplash.com/photos/pE9mgLMwee0
- Kendall Ruth https://unsplash.com/photos/AijuW-HlE30

<a name=""></a>
## P.S.

If you want more articles (like this) on this blog, then post a comment below and subscribe to me. Thanks! 😻

And of course, you can help me make developers' lives even better! Just connect to one of my projects as a contributor. It's easy!

My projects that need your help (and stars) 👇

- 🔥 [gowebly][gowebly_url]: A next-generation CLI tool for easily build amazing web applications with Go on the backend, using htmx & hyperscript and the most popular atomic/utility-first CSS frameworks on the frontend.
- ✨ [create-go-app][cgapp_url]: Create a new production-ready project with Go backend, frontend and deploy automation by running one CLI command.
- 🏃 [yatr][yatr_url]: Yet Another Task Runner allows you to organize and automate your routine operations that you normally do in Makefile (or else) for each project.
- 📚 [gosl][gosl_url]: The Go Snippet Library provides snippets collection for working with routine operations in your Go programs with a super user-friendly API and the most efficient performance.
- 🏄‍♂️ [csv2api][csv2api_url]: The parser reads the CSV file with the raw data, filters the records, identifies fields to be changed, and sends a request to update the data to the specified endpoint of your REST API.
- 🚴 [json2csv][json2csv_url]: The parser can read given folder with JSON files, filtering and qualifying input data with intent & stop words dictionaries and save results to CSV files by given chunk size.

<!-- Footer links -->

[gowebly_url]: https://github.com/gowebly
[cgapp_url]: https://github.com/create-go-app
[yatr_url]: https://github.com/koddr/yatr
[gosl_url]: https://github.com/koddr/gosl
[csv2api_url]: https://github.com/koddr/csv2api
[json2csv_url]: https://github.com/koddr/json2csv