georgysavva / scany

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

Is scany thread safe ? #136

Open charbelcs123 opened 1 month ago

charbelcs123 commented 1 month ago

type ServiceOverview struct { ServiceId uint64 db:"ID" SubscriptionId uint64 db:"SUB_ID" StatusId int32 db:"STATUS_ID" }

const SelectSubscriptionOverviewSql string = SELECT TO_CHAR(s.id) as ID, TO_CHAR(s.SUBSCRIPTION_ID) as SUB_ID, s.STATUS_ID FROM Service

func (d *Dao) GetServiceOverviews(ctx context.Context, accountId uint64, bgId uint64) ([]ServiceOverview, error) {

var sos []ServiceOverview
err := sqlscan.Select(ctx, d.rDb, &sos, SelectSubscriptionOverviewSql, accountId, bgId)
return sos, err

}

I am getting the following error: scanning one: scanning: doing scan: scanFn: scany: column: 'A_NUMBER': no corresponding field found, or it's unexported in models.ServiceOverview

Please note that A_NUMBER is not present in SelectSubscriptionOverviewSql

georgysavva commented 1 month ago

Hi, this is an interesting issue. Yes, scany is thread-safe, as long as your underlying driver library instance, d.rDb, is thread-safe. To debug this, I would recommend printing SelectSubscriptionOverviewSql and executing it on the database directly (without scany) to see what columns it returns right before where you get the error.

charbelcs123 commented 1 month ago

Ok, Thank you for clearing that out, Usually the query works, and then the process starts crashing after a while with random errors for different queries Maybe i am missing something, I am not sure.