Skip to content

sqlc vet/generate fails on alter type migrations with the relation does not exist error #3997

Open
@Aeron

Description

@Aeron

Version

1.29.0

What happened?

Hey there. I’m working on a project that relies on Postgres and Atlas, so sqlc is configured to get the schema from the migrations directory. Recently, I was implementing a feature that requires a new field for a composite type, so — naturally — I’ve created a new migration with a simple statement, like so:

alter type something.my_type
add attribute name text;

It’s perfectly valid code for Postgres. Yet the first sqlc vet command gave me the relation "my_type" does not exist error. So I started digging and — after all the troubleshooting and searches on the topic — it’s clear that sqlc doesn’t know how to handle alter type statements properly. At least, the alter type … add attribute ones. Thus, it’s a bug.

The playground link has a reproducible example with a single schema.sql that mimics migrations. It doesn’t matter if the type’s name is something.my_type or just my_type.

All the provided code is simplified, shortened, and redacted, of course.

There are workarounds, but they are sub-optimal. The two off the top of my head:

  • Start generating schema.sql, so sqlc faces only the final form of all the migrations. Which is a bit challenging when Atlas historically lives in a container and the team has a single workflow around migrations;
  • Drop and re-create the composite type completely. Which means everything relying on the type must be migrated as well.

Relevant log output

# package
migrations/20250620000000_my_type_name.sql:1:1: relation "my_type" does not exist
# package
migrations/20250620000000_my_type_name.sql:1:1: relation "my_type" does not exist
# package
migrations/20250620000000_my_type_name.sql:1:1: relation "my_type" does not exist

Database schema

create type my_type as (
    name text
);

create table example (
    data my_type not null
);

SQL queries

Do not matter here.

Configuration

version: "2"
sql:
  - engine: "postgresql"
    queries: "sqlc/queries"
    schema: "migrations"
    gen:
      go:
        sql_package: "pgx/v5"
        out: "sqlc/repos"
        emit_pointers_for_null_types: true
        emit_interface: true
        emit_enum_valid_method: true
        emit_result_struct_pointers: true
        emit_params_struct_pointers: true
        emit_json_tags: true
        omit_unused_structs: true

Playground URL

https://play.sqlc.dev/p/58d7ab358bfa99770835ca29c4a20546ec7ccbed0414e66f129d85db4bbb90b8

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