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

Error "not enough args to execute query" on complex select query. #3359

Closed
intaek-h opened this issue Apr 29, 2024 · 0 comments
Closed

Error "not enough args to execute query" on complex select query. #3359

intaek-h opened this issue Apr 29, 2024 · 0 comments
Labels
bug Something isn't working triage New issues that hasn't been reviewed

Comments

@intaek-h
Copy link

Version

1.26.0

What happened?

-- name: GetJobPostings :many
SELECT
    *
FROM
    JobPostings
WHERE
    created_at >= ?
    AND job_type = COALESCE(sqlc.narg ('job_type'), job_type)
    AND salary >= COALESCE(sqlc.narg ('salary'), salary)
    AND gender = COALESCE(sqlc.narg ('gender'), gender)
    AND posting_type = COALESCE(sqlc.narg ('posting_type'), posting_type)
    AND max_age = COALESCE(sqlc.narg ('max_age'), max_age)
    AND work_hours_type = COALESCE(sqlc.narg ('work_hours_type'), work_hours_type)
    AND work_days_type = COALESCE(sqlc.narg ('work_days_type'), work_days_type)
ORDER BY
    created_at DESC
LIMIT
    ?
OFFSET
    ?;

generated:

type GetJobPostingsParams struct {
	CreatedAt     time.Time
	JobType       sql.NullInt64
	Salary        sql.NullInt64
	Gender        sql.NullString
	PostingType   sql.NullInt64
	MaxAge        sql.NullInt64
	WorkHoursType sql.NullInt64
	WorkDaysType  sql.NullInt64
	Limit         int64
	Offset        int64
}

my code:

	postings, err := p.DB.GetJobPostings(r.Context(), database.GetJobPostingsParams{
		CreatedAt:     time.Now().AddDate(0, 0, -7),
		JobType:       sql.NullInt64{Int64: 0, Valid: false},
		Salary:        sql.NullInt64{Int64: 0, Valid: false},
		Gender:        sql.NullString{String: "", Valid: false},
		PostingType:   sql.NullInt64{Int64: 0, Valid: false},
		MaxAge:        sql.NullInt64{Int64: 0, Valid: false},
		WorkHoursType: sql.NullInt64{Int64: 0, Valid: false},
		WorkDaysType:  sql.NullInt64{Int64: 0, Valid: false},
		Offset:        0,
		Limit:         4,
	})
	if err != nil {
		fmt.Printf("Error: %v\n", err)
		http.Error(w, "Internal Server Error", http.StatusInternalServerError)
		return
	}

Relevant log output

Error: not enough args to execute query: want 12 got 10

Database schema

CREATE TABLE JobPostings (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    user_id INTEGER NOT NULL,
    created_at TIMESTAMP NOT NULL,
    updated_at TIMESTAMP NOT NULL,
    title TEXT NOT NULL,
    short_description TEXT NOT NULL,
    full_description TEXT,
    job_type INTEGER NOT NULL, 
    salary INTEGER,
    salary_type TEXT, 
    gender TEXT, 
    posting_type INTEGER NOT NULL,
    max_age INTEGER, 
    work_hours_type INTEGER, 
    work_days_type INTEGER, 
    work_hours_description TEXT, 
    address TEXT, 
    map_url TEXT, 
    foreign key (user_id) references Users (id)
);

SQL queries

-- name: GetJobPostings :many
SELECT
    *
FROM
    JobPostings
WHERE
    created_at >= ?
    AND job_type = COALESCE(sqlc.narg ('job_type'), job_type)
    AND salary >= COALESCE(sqlc.narg ('salary'), salary)
    AND gender = COALESCE(sqlc.narg ('gender'), gender)
    AND posting_type = COALESCE(sqlc.narg ('posting_type'), posting_type)
    AND max_age = COALESCE(sqlc.narg ('max_age'), max_age)
    AND work_hours_type = COALESCE(sqlc.narg ('work_hours_type'), work_hours_type)
    AND work_days_type = COALESCE(sqlc.narg ('work_days_type'), work_days_type)
ORDER BY
    created_at DESC
LIMIT
    ?
OFFSET
    ?;

Configuration

version: "2"
sql:
  - engine: "sqlite"
    queries: "sql/queries"
    schema: "sql/schema"
    gen:
      go:
        out: "internal/database"

Playground URL

No response

What operating system are you using?

macOS

What database engines are you using?

SQLite

What type of code are you generating?

Go

@intaek-h intaek-h added bug Something isn't working triage New issues that hasn't been reviewed labels Apr 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working triage New issues that hasn't been reviewed
Projects
None yet
Development

No branches or pull requests

1 participant