Skip to content

Unable to write CIDR directly to database - indirect typecasting required #1588

@wburnett

Description

@wburnett

Version

1.12.0

What happened?

I was unable to use an update method generated by SQLC when the type in question was CIDR.
I was able to jury-rig a valid entry in the database by first casting the struct as varchar and then as CIDR in directly (cast(@internal_cidr as varchar)::cidr) but this way feels messy.

Is there a better way to update a CIDR field directly in the database without two layers of casting?

Relevant log output

Error:      	Received unexpected error:
sql: converting argument $2 type: unsupported type pqtype.CIDR, a struct

Database schema

create extension if not exists "uuid-ossp";

create table public.vpns
(
	"id" uuid default uuid_generate_v4() not null primary key,
	"internal_cidr" cidr,
	"deleted_at" timestamptz default null
    -- snip other unrelated fields
);

SQL queries

-- this one fails with the above error log
-- name: UpdateInternalCidr :one
update vpns set internal_cidr = $2
where id = $1 and deleted_at is null returning *;
-- this workaround works
-- name: UpdateInternalCidr :one
update vpns set internal_cidr = cast(@internal_cidr as varchar)::cidr
where id = $1 and deleted_at is null returning *;

Configuration

version: 1
packages:
  - path: "services/repository"
    name: "repository"
    engine: "postgresql"
    schema: "./sql/schema.sql"
    queries: "./sql/query.sql"
    emit_json_tags: true

Playground URL

https://play.sqlc.dev/p/5c2872fea6ed7a6c12c1e0e7640d06b3f56902b16d2da04b8554627fcc2cd68d

What operating system are you using?

macOS

What database engines are you using?

PostgreSQL

What type of code are you generating?

Go

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions