【问题标题】:Scan PostgresQl enum to Protobuf enum扫描 PostgresQl 枚举到 Protobuf 枚举
【发布时间】:2020-08-17 21:07:13
【问题描述】:

我已经在表格中归档了枚举。当我在 sqlxGet() 方法的帮助下进行 SELECT 查询时,我得到:

sql:列索引 1 上的扫描错误,名称“type”:将 driver.Value 类型字符串(“INDIVIDUAL”)转换为 int32:语法无效

Postgres 表:

create type account_type as enum ('INDIVIDUAL', 'BUSINESS');
create table account (
    id varchar not null primary key,
    type account_type not null,
    email varchar(254) not null unique
);

原型文件的一部分:

enum AccountType {
    INDIVIDUAL = 0;
    BUSINESS = 1;
}

message Account {
    string id = 1;
    AccountType type = 2;
    string email = 3;
}

SQL 查询:

SELECT id, type, email
FROM account
WHERE email = $1
LIMIT 1

如何将 PostgresQL 枚举扫描到 Protobuf 枚举?我必须实现自己的扫描仪还是有其他方法?

【问题讨论】:

    标签: sql postgresql enums protocol-buffers sqlx


    【解决方案1】:

    我不知道它是否有什么不同(因为我完全不知道 GO),但发布的错误表明它可能会。 Postgres 枚举排序由浮点数(enumsortorder)控制,而不是整数。

    postgres=# \d  pg_enum
                  Table "pg_catalog.pg_enum"
        Column     | Type | Collation | Nullable | Default
    ---------------+------+-----------+----------+---------
     enumtypid     | oid  |           | not null |
     enumsortorder | real |           | not null |
     enumlabel     | name |           | not null |
    Indexes:
        "pg_enum_oid_index" UNIQUE, btree (oid)
        "pg_enum_typid_label_index" UNIQUE, btree (enumtypid, enumlabel)
        "pg_enum_typid_sortorder_index" UNIQUE, btree (enumtypid, enumsortorder)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-10
      • 1970-01-01
      • 2013-01-29
      • 2015-05-23
      • 2020-05-21
      相关资源
      最近更新 更多