sqlc-dev / sqlc

Generate type-safe code from SQL
https://sqlc.dev
MIT License
13.51k stars 810 forks source link

Problem with schema switching in PostgreSQL when running sqlc queries (relation does not exist error #3713

Closed sunmery closed 3 days ago

sunmery commented 3 days ago

Version

1.27.0

What happened?

no error

Relevant log output

sqlc generate
# package data
internal/data/query/addresses.sql:4:13: relation "addresses" does not exist
internal/data/query/addresses.sql:15:1: relation "addresses" does not exist
internal/data/query/addresses.sql:20:1: relation "addresses" does not exist
internal/data/query/addresses.sql:20:8: relation "addresses" does not exist
internal/data/query/addresses.sql:31:6: relation "addresses" does not exist
internal/data/query/addresses.sql:38:6: relation "addresses" does not exist

Database schema

-- CREATE SCHEMA IF NOT EXISTS addresses; I have performed the schema creation in the database

SET search_path TO addresses;

CREATE TABLE addresses
(
    id SERIAL PRIMARY KEY,
    user_id        VARCHAR(100) NOT NULL, 
    street_address TEXT         NOT NULL,
    state          VARCHAR(20)  NOT NULL, 
    country        VARCHAR(100) NOT NULL, 
    zip_code       int4         NOT NULL  
);

ALTER TABLE addresses
    ADD
        FOREIGN KEY (user_id) REFERENCES public.user (id);

CREATE INDEX idx_addresses_user_id ON addresses(user_id);

SQL queries

-- name: CreatUserAddress :one
INSERT INTO addresses.addresses(user_id, street_address, city, state, country, zip_code)
VALUES ($1, $2, $3, $4, $5, $6)
RETURNING *;

-- name: GetUserAddress :one
SELECT *
FROM addresses.addresses
WHERE user_id = sqlc.arg(user_id)
LIMIT 1;

-- name: ListUserAddresses :many
SELECT *
FROM addresses.addresses
WHERE user_id = sqlc.arg(user_id);

-- name: UpdateUserAddress :one
UPDATE addresses.addresses
SET street_address = COALESCE(sqlc.narg(street_address), street_address),
    city           = COALESCE(sqlc.narg(city), city),
    state          = COALESCE(sqlc.narg(state), state),
    country        = COALESCE(sqlc.narg(country), country),
    zip_code       = COALESCE(sqlc.narg(zip_code), zip_code)
WHERE user_id = sqlc.arg(user_id)
RETURNING *;

-- name: DeleteUserAddress :one
DELETE
FROM addresses.addresses
WHERE user_id = sqlc.arg(user_id)
  AND id = sqlc.arg(addresses.addresses_id)
RETURNING *;

-- name: DeleteUserAddresses :one
DELETE
FROM addresses.addresses
WHERE user_id = sqlc.arg(user_id)
RETURNING *;

Configuration

version: "2"
sql:
  - schema: "internal/data/schema/addresses.sql"
    queries: "internal/data/query/addresses.sql"
    engine: "postgresql"
    database:
      uri: postgresql://postgres:postgres@localhost:5432/example?sslmode=disable"
    gen:
      go:
        package: "data"
        out: "internal/data"
        sql_package: "pgx/v5"

Playground URL

https://play.sqlc.dev/p/47de9fa1a290eabd6215a5fe09963dbf3a30c8e8cc94ba1d6e43bbcd5fd0b0aa

What operating system are you using?

macOS

What database engines are you using?

PostgreSQL

What type of code are you generating?

Go