BamBam22
BamBam22

Reputation: 713

SQLC generate: 'relation "users" does not exist'

I have a directory of migrations. One of those migration files has the name of 20240718160024_initial_tables.sql This migration file contains the sql to create the users table. When I run sqlc generate i get the following error.

db/queries/users.sql:1:1: relation "users" does not exist

Now, if I create another file called "20240718160024_users.sql" to create the users table then it works. Here is my sqlc.yml

version: "2"
sql:
  - name: "db"
    queries: "./db/queries"
    schema: "./db/migrations"
    engine: "postgresql"
    gen:
      go:
        package: "sqlc"
        out: "./internal/database/sqlc"
        sql_package: "pgx/v5"

And my users.sql query

-- name: GetUserByID :one
SELECT
    id,
    email,
    first_name,
    last_name,
    date_of_birth,
    phone_number,
    terms_accepted,
    created_at,
    updated_at
FROM users
WHERE id = $1
limit 1;

20240718160024_initial_tables.sql

-- +goose Up
-- +goose StatementBegin
create table if not exists users (
    id serial primary key,
    email varchar(255) unique not null,
    first_name varchar(100) not null,
    last_name varchar(100) not null,
    date_of_birth date not null,
    phone_number varchar(15) not null,
    terms_accepted boolean not null default false,
    created_at timestamp with time zone default current_timestamp,
    updated_at timestamp with time zone default current_timestamp
);
do $$
    begin
        if not exists (select 1 from pg_constraint where conname = 'email_format_chk')
        then alter table users add constraint email_format_chk check (email ~* '^[a-za-z0-9._%+-]+@[a-za-z0-9.-]+\.[a-za-z]{2,}$');
        end if;
    end
$$;
select set_updated_at_trigger('users');
-- +goose StatementEnd

-- +goose Down
-- +goose StatementBegin
drop table users;
-- +goose StatementEnd

Upvotes: 1

Views: 218

Answers (0)

Related Questions