skip to Main Content

users.sql

-- name: CreateUser :one
INSERT INTO "users" ("id", "created_at", "updated_at", "name")
VALUES ($1, $2, $3, $4)
RETURNING *;

sqlc.yaml

version: "2"
sql:
  - engine: "postgresql"
    queries: "sql/queries"
    schema: "sql/schema"
    gen:
      go:
        # package: "internal"
        out: "internal/database"
        sql_package: "pgx/v5"

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";

when i try to generate i get an error like this

$ sqlc generate
# package 
sqlqueriesusers.sql:1:1: relation "users" does not exist

is anyone know how to solve this error.

i try every that is it’s not coming

but for som one or two tables creating sqlc generate works

-- name: GetPost :one
SELECT * FROM post
WHERE id = $1 LIMIT 1;

-- +goose Up
CREATE TABLE post (
    id int NOT NULL,
    title text,
    body text,
    PRIMARY KEY(id)
);

-- +goose Down
DROP TABLE post;

created sqlc generate for post

this is my sqlc.yaml

version: "2"
sql:
  - engine: "postgresql"
    queries: "sqldb/queries"
    schema: "sqldb/schema"
    gen:
      go:
        package: "db"
        out: "migrations"
        # sql_package: "pgx/v5"

i deleted the sql folder and create a new db called rss and sqldb folder then according to document in the goose i get that and name it 001_post.sql and in query i pasted the post.sql in the document.

and again i tried the goose up comment then the new post table crated in my new db rss.

then i came to the root folder i ran sqlc generate then it’s generate the code same exactly i did for the user again and i get error.

2

Answers


  1. The reason this might happen is because of the table name mismatch in the "001_user.sql" and in the "users.sql". Make sure both the names match and then run: sqlc generate

    Login or Signup to reply.
  2. The schema key in the sqlc.yaml file should point to your database migrations path, in this case the path to the 001_users.sql file. I had the same error but this fixed it. Here is how my configuration looks like after.

    sqlc.yaml

    version: "2"
    sql:
      - engine: "postgresql"
        queries: "./db/query"
        schema: "./db/migrations"
        gen:
          go:
            package: "db"
            out: "./db/sqlc/"
            sql_package: "pgx/v5"
            emit_json_tags: true
    

    db/query/account.sql

    -- name: CreateAccount :one
    INSERT INTO accounts (
      owner,
      balance,
      currency
    ) VALUES (
      $1, $2, $3
    )
    RETURNING *;
    

    db/migrations/000001_init_schema.up.sql

    CREATE TABLE "accounts" (
      "id" bigserial PRIMARY KEY,
      "owner" varchar NOT NULL,
      "balance" bigint NOT NULL,
      "currency" varchar NOT NULL,
      "created_at" timestamptz NOT NULL DEFAULT (now())
    );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search