繁体   English   中英

扫描 PostgresQl 枚举到 Protobuf 枚举

[英]Scan PostgresQl enum to Protobuf enum

我已经在表格中归档了枚举。 当我在sqlx Get() 方法的帮助下执行 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 枚举? 我必须实现自己的扫描仪还是有其他方法?

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

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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM