Klimbo
Klimbo

Reputation: 1448

Scan PostgresQl enum to Protobuf enum

I have enum filed in the table. When I do a SELECT query with the help of sqlx Get() method, I get:

sql: Scan error on column index 1, name "type": converting driver.Value type string ("INDIVIDUAL") to a int32: invalid syntax

Postgres table:

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
);

A part of the proto file:

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

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

SQL query:

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

How to scan PostgresQL enum to Protobuf enum? Do I have to implement my own scanner or there is another way?

Upvotes: 3

Views: 655

Answers (1)

Belayer
Belayer

Reputation: 14934

I don't know if it makes any much difference (as I'm totally ignorant of GO) but the error posted indicates it might. Postgres enum ordering is controled by a floating point (enumsortorder), not an 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)

Upvotes: 0

Related Questions