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

发布于 2025-01-11 22:47:39 字数 1189 浏览 3 评论 0原文

我是 golang 和 pgx 的新手,当我尝试运行简单的查询时遇到了问题。我在 postgres 中有下表。

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] 转换为文本。

编辑 03/05/2022

我应该注意,我将 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)
}

我假设类型信息发生了一些问题,但仍然不知道如何修复它。

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

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 {}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

听闻余生 2025-01-18 22:47:39

您忘记将 ... 添加到传递给 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.

寒冷纷飞旳雪 2025-01-18 22:47:39

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

我认为这是由于 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文