Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sqlc.narg is not being added to parms #2324

Closed
k1ng440 opened this issue Jun 13, 2023 · 3 comments
Closed

sqlc.narg is not being added to parms #2324

k1ng440 opened this issue Jun 13, 2023 · 3 comments

Comments

@k1ng440
Copy link

k1ng440 commented Jun 13, 2023

Version

1.18.0

What happened?

A field should be added to params but it does not

Relevant log output

const productListByOwner = `-- name: ProductListByOwner :many
SELECT id, owner_id, created_by, name, slug, category_id, vendor_id, tags, created_at, updated_at, published_at, deleted_at
FROM products
WHERE owner_id = $1
AND (deleted_at IS NOT NULL OR $4 IS NULL)
ORDER BY id ASC
LIMIT $2 OFFSET $3
`

type ProductListByOwnerParams struct {
	OwnerID int32
	Limit   int32
	Offset  int32
}

func (q *Queries) ProductListByOwner(ctx context.Context, arg *ProductListByOwnerParams) ([]Product, error) {
	rows, err := q.db.QueryContext(ctx, productListByOwner, arg.OwnerID, arg.Limit, arg.Offset)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []Product
	for rows.Next() {
		var i Product
		if err := rows.Scan(
			&i.ID,
			&i.DeletedAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

Database schema

CREATE TABLE products
(
    id           SERIAL PRIMARY KEY,
    owner_id     INT NOT NULL,
    created_by   INT  NOT NULL,
    name         VARCHAR(100)               NOT NULL,
    slug         VARCHAR(255)               NOT NULL,
    created_at   TIMESTAMP                  NOT NULL DEFAULT NOW(),
    updated_at   TIMESTAMP                  NOT NULL DEFAULT NOW(),
    published_at TIMESTAMP,
    deleted_at   TIMESTAMP
);

Configuration

version: "2"
sql:
  - engine: "postgresql"
    queries: "internal/db/query.sql"
    schema: "internal/db/schema.sql"
    gen:
      go:
        package: "db"
        out: "internal/db"
        emit_params_struct_pointers: true

Playground URL

https://play.sqlc.dev/p/f2ae3b0558f20a7040d2aeb039f30d7c797b51fe6b2c67fcdc3522d838024a24

What operating system are you using?

Linux

What database engines are you using?

PostgreSQL

What type of code are you generating?

Go

@k1ng440 k1ng440 added bug Something isn't working triage New issues that hasn't been reviewed labels Jun 13, 2023
@kyleconroy kyleconroy added 📚 postgresql 🔧 golang 💻 linux and removed triage New issues that hasn't been reviewed labels Jun 15, 2023
@orisano
Copy link
Contributor

orisano commented Aug 3, 2023

In this case, the problem seems to occur because the type of sqlc.narg cannot be determined.
If you explicitly specify the type as follows, it works.
https://play.sqlc.dev/p/7037e328e07626afa86b94322ef813279c5649f72e6f062b28b07e3deab8837f

What type did you expect sqlc.narg('DeletedAt') to be?

@kyleconroy
Copy link
Collaborator

When running this query through PostgreSQL, we get the following error:

queries/products.sql:1:1: ERROR: could not determine data type of parameter $4 (SQLSTATE 42P18)

That said, sqlc shouldn't drop the parameter completely, as that will result in a query error.

@kyleconroy
Copy link
Collaborator

Fixed in #2844

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants