Go和pgx:无法将[+11111111111]转换为Text

huangapple go评论79阅读模式
英文:

Go and pgx: cannot convert [+11111111111] to Text

问题

我是你的中文翻译助手,以下是翻译好的内容:

我对golang和pgx都不太熟悉,当我尝试运行一个简单的查询时遇到了问题。我在PostgreSQL中有以下表格:

CREATE TABLE users (
	user_id BIGINT PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
	phone_number TEXT NOT NULL UNIQUE
);

当我尝试运行以下查询时:

func sampleFunc(db dbClient) {
	number := "+111111111"
	rows, err := db.Query(context.Background(), "SELECT user_id FROM users WHERE phone_number=$1::TEXT;", number)
	if err != nil {
		log.Println(err)
		return false, err
	}
}

我得到了以下错误:无法将[+111111111]转换为Text。

编辑于2022年3月5日

我应该注意到我在自己的结构体中封装了pgxpool.Pool,但是当我直接使用pgxpool时,实际上没有任何问题。

type dbClient interface {
	Query(ctx context.Context, query string, args ...interface{}) (pgx.Rows, error)
}

type SQLClient struct {
	Conn *pgxpool.Pool
}

func (db *SQLClient) Query(ctx context.Context, query string, args ...interface{}) (pgx.Rows, error) {
	return db.Conn.Query(ctx, query, args)
}

我猜想可能是类型信息出了问题,但仍然无法弄清楚如何解决。

当我在SQLClient.Query中打印args的类型信息时,使用fmt.Println(reflect.TypeOf(args)),我得到了以下结果:[]interface {}

英文:

I'm new to golang and pgx and I'm running into an issue when I try to run a simple query. I have the following table in postgres.

CREATE TABLE users (
	user_id BIGINT PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
	phone_number TEXT NOT NULL UNIQUE, 
);

When I try to run the following query:

func sampleFunc(db dbClient){
	number := "+111111111"
	rows, err := db.Query(context.Background(), "SELECT user_id FROM users WHERE phone_number=$1::TEXT;", number)
	if err != nil {
		log.Println(err)
		return false, err
	}
}

I get the following error: cannot convert [+111111111] to Text.

EDIT 03/05/2022

I should note that I'm wrapping the pgxpool.Pool in my own struct, and for some reason there's actually no issue when I use the pgxpool directly.

type dbClient interface {
	Query(ctx context.Context, query string, args ...interface{}) (pgx.Rows, error)
}

type SQLClient struct {
	Conn *pgxpool.Pool
}

func (db *SQLClient) Query(ctx context.Context, query string, args ...interface{}) (pgx.Rows, error) {
	return db.Conn.Query(ctx, query, args)
}

I'm assuming that then something is happening with the type information, but still can't figure out how to go about fixing it.

When I print the args type info in SQLClient.Query, by using fmt.Println(reflect.TypeOf(args)), I get the following: []interface {}

答案1

得分: 3

你忘记在传递给db.Conn.Query()args参数中添加...

func (db *SQLClient) Query(ctx context.Context, query string, args ...interface{}) (pgx.Rows, error) {
    return db.Conn.Query(ctx, query, args...)
}

规范

英文:

You forgot to add ... to the args argument passed to db.Conn.Query().

func (db *SQLClient) Query(ctx context.Context, query string, args ...interface{}) (pgx.Rows, error) {
    return db.Conn.Query(ctx, query, args...)
}

The spec.

答案2

得分: 0

我在这里看到了相同的问题

> 我认为这是由于pgx内部使用预处理语句和PostgreSQL无法确定占位符类型的组合引起的。最简单的解决方案是在占位符中包含类型信息,例如$1::int。或者,您可以配置pgx使用简单协议而不是预处理语句。

英文:

I saw the same problem here

> I think this is caused by the combination of pgx internally using prepared statements and PostgreSQL not being able to determine the type of the placeholders. The simplest solution is to include type information with your placeholders. e.g. $1::int. Alternatively, you could configure pgx to use the simple protocol instead of prepared statements.

huangapple
  • 本文由 发表于 2022年3月5日 07:22:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/71358226.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定