https://github.com/ik5/example_pgx_with_complext_oid
Example on how to use complex domain using golang and pgx
https://github.com/ik5/example_pgx_with_complext_oid
example golang pgx pgxpool poc postgresql
Last synced: 11 months ago
JSON representation
Example on how to use complex domain using golang and pgx
- Host: GitHub
- URL: https://github.com/ik5/example_pgx_with_complext_oid
- Owner: ik5
- Created: 2024-02-09T12:05:21.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-02-09T12:27:21.000Z (almost 2 years ago)
- Last Synced: 2025-02-02T07:14:03.606Z (about 1 year ago)
- Topics: example, golang, pgx, pgxpool, poc, postgresql
- Language: Go
- Homepage:
- Size: 2.93 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.markdown
Awesome Lists containing this project
README
The following code helps me to understand how to use complex domain in PostgreSQL using [pgx v5](https://github.com/jackc/pgx).
In this case I used an array of another domain:
```SQL
CREATE DOMAIN public.http_client_error_code AS smallint
CONSTRAINT http_client_error_code_check CHECK (VALUE = 0 OR VALUE >= 400 AND VALUE <= 499);
```
The above domain is a simple integer with a constraint.
```SQL
CREATE DOMAIN public.http_client_error_code_list AS http_client_error_code[];
```
The above domain is an array of the first domain (`http_client_error_code`).
When trying to scan or assign it using pgx an error will be raised:
```
panic: failed to encode args[0]: unable to encode main.HTTPClientErrorCodeList{400} into text format for unknown type (OID 32783): cannot find encode plan
```
The issue is that `pgx` does not understand how to convert the slice into `http_client_error_code_list`.
By telling `pgx` to look for that domain (and given OID), it know how to `scan` and `value` the Go's data type.