georgysavva / scany

Library for scanning data from a database into Go structs and more
MIT License
1.23k stars 67 forks source link

Possible Query Limit? #105

Closed edewindt closed 1 year ago

edewindt commented 1 year ago

This query doesn't work:

type Invoice struct {
    ID int32 `json:"id"`
    Month string `json:"month"`
    Sheet []Sheet `json:"sheet"`
    Firstname string `json:"firstname"`
    Lastname string `json:"lastname"`
    TotalHours int32 `json:"total_hours"`
}

sqlQuery := "Select students.id, (sheet -> 0 ->> 'start_time')::timestamp::date as month, sheet, firstname, lastname, total_hours from attendance join students on attendance.student_id = students.id join agencies on students.agency_id = agencies.id WHERE (sheet -> 0 ->> 'start_time')::timestamp::date >= (SELECT date_trunc('month', '2023-01-17'::date)::date)AND (sheet -> 0 ->> 'start_time')::timestamp::date <=  (SELECT date_trunc('month', '2023-01-17'::date + interval '1 month - 1 day')::date)"

var invoice []*Invoice
err := pgxscan.Select(ctx, db, &invoices, sqlQuery) 

But this one does:

type Invoice struct {
    ID int32 `json:"id"`
    Sheet []Sheet `json:"sheet"`
    Firstname string `json:"firstname"`
    Lastname string `json:"lastname"`
    TotalHours int32 `json:"total_hours"`
}

sqlQuery := "Select students.id, sheet, firstname, lastname, total_hours from attendance join students on attendance.student_id = students.id join agencies on students.agency_id = agencies.id WHERE (sheet -> 0 ->> 'start_time')::timestamp::date >= (SELECT date_trunc('month', '2023-01-17'::date)::date)AND (sheet -> 0 ->> 'start_time')::timestamp::date <=  (SELECT date_trunc('month', '2023-01-17'::date + interval '1 month - 1 day')::date)"

var invoice []*Invoice
err := pgxscan.Select(ctx, db, &invoices, sqlQuery) 

Is there some sort of limit to what SQL queries I can pass in? Because this SQL Query will work if I query the database directly, but not through golang.

edewindt commented 1 year ago

Well now neither work, the second one had been working for days, and then all of the sudden started returning null like the other one, and I already built a ton of my application around it... anyway to fix this, or should I just use the native driver pgx itself?

edewindt commented 1 year ago

Nevermind, it was a bug on my end, I was trying to run a SQL function on a subquery on data that didn't exist

georgysavva commented 1 year ago

Hi @edewindt. I am glad you solved this. I am closing the issue. Feel free to reopen or create a new one if you need help in the future.