将PostgreSQL枚举类型转换为Protobuf枚举类型

3

我有一个表格中的枚举字段。当我使用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
);

proto文件的一部分:

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枚举类型?我是否需要自己实现扫描器,还是有其他方法?
1个回答

0

我不知道这是否有太大的区别(因为我对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)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接