Even though, the PostgreSQL database has "users" table whose schema is "public", and confirmed to have been ran in the migration, the "sqlc generate" command returns "# package sql/queries/users.sql:4:1: relation "users" does not exist" error.
sqlc.yaml
version: "2"
sql:
- schema: "sql/schema"
queries: "sql/queries"
engine: "postgresql"
gen:
go:
out: "internal/database"
sql/queries/users.sql
-- name: CreateUser :one
INSERT INTO users (id, created_at, updated_at, name)
VALUES ($1, $2, $3, $4)
RETURNING *;
sql/schema/001_users.sql
-- +goose up
CREATE TABLE users (
id UUID PRIMARY KEY,
created_at TIMESTAMP NOT NULL,
updated_at TIMESTAMP NOT NULL ,
name TEXT NOT NULL
);
-- +goose down
DROP TABLE users;
Goose annotations (e.g. +goose Up
) were case sensitive until recently, sqlc has not been updated to reflect this change.
To fix your code generation, simply change the annotations to use Up
and Down
(first letter upper-case) i.e.
-- +goose Up
CREATE TABLE users (
id UUID PRIMARY KEY,
created_at TIMESTAMP NOT NULL,
updated_at TIMESTAMP NOT NULL ,
name TEXT NOT NULL
);
-- +goose Down
DROP TABLE users;
The code in your question does not work because sqlc processes the entire file (it does not remove the bit below -- +goose down
), so the users
table is created and then removed (meaning it does not exist when the query is processed). Using the expected case resolves this.