I am using Go 1.13 Postgres 11 and GraphQL
I am trying to run three different queries: 1. To get details of few employees when some condition is true. 2. To get all details of all employees. 3. To get all details of an employee when matches the Empid.
When i run my code, it compiles successfully
But when i call the API for (2 and 3) it says failed to fetch. API for 1 runs successfully.
On browsing for solution and trying to find where the error happens, it converges at rows.Scan() function.
When i comment rows.Scan() line, it runs without giving data but when runned after un-commenting
Shows error : Failed to fetch.
So the error is with rows.Scan()
Maybe we cannot have more than one rows.Scan() in a project. Thinking of this i created a function that takes a querystatement and returns an interface.
But this also didnot worked.
connStr := fmt.Sprintf("port=%d host=%s user=%s password=%s dbname=%s sslmode=disable", hostport, hostname, username, password, databaseName)
db, err := sql.Open("postgres", connStr)
if err != nil {
fmt.Println(`Could not connect to db`)
panic(err)
}
defer db.Close()
rows, err := db.Query(query)
if err != nil {
panic(err)
}
defer rows.Close()
for rows.Next() {
row := models.Universal{}
if num == 1 {
if err := rows.Scan(&row.AdhaarNo, &row.PersonalMobile, &row.Branch, &row.EmpID, &row.EmpName, &row.DOB, &row.DOJ, &row.Gender, &row.LocAdd1, &row.LocAdd2, &row.LocCity, &row.LocState, &row.LocCountry, &row.PerAdd1, &row.PerAdd2, &row.PerCity, &row.PerState, &row.PerCountry, &row.OfficeMail, &row.OfficeMobile, &row.LocPIN, &row.PerPIN, &row.MotherName, &row.PFNo, &row.BloodGroup, &row.PAN, &row.PersonalEmail, &row.FatherName, &row.EmergencyNo, &row.BankAcNo, &row.EmpBank, &row.IFSC, &row.Designation, &row.OnboardingStatus); err != nil {
log.Fatal(err)
}
models.AllUniversal = append(models.AllUniversal, row)
} else if num == 2 {
if err := rows.Scan(&row.AdhaarNo, &row.PersonalMobile, &row.Branch, &row.EmpID, &row.EmpName, &row.DOB, &row.DOJ, &row.Gender, &row.LocAdd1, &row.LocAdd2, &row.LocCity, &row.LocState, &row.LocCountry, &row.PerAdd1, &row.PerAdd2, &row.PerCity, &row.PerState, &row.PerCountry, &row.OfficeMail, &row.OfficeMobile, &row.LocPIN, &row.PerPIN, &row.MotherName, &row.ESINo, &row.PAN, &row.VisaNo, &row.VisaExpiry, &row.IntegreationRef, &row.OrgID, &row.DeptID, &row.SectionID, &row.JobTypeID, &row.GradeID, &row.DesignationID, &row.CustomGroup1ID, &row.CustomGroup2ID, &row.CustomGroup3ID, &row.Card1, &row.Card2, &row.ScheduleGroupID, &row.StartShift, &row.PersonalEmail, &row.WeakOffGropuID, &row.LeaveGroup, &row.Field1, &row.PFNo, &row.BloodGroup, &row.SerialNo, &row.PassportNo, &row.PassportExpiry, &row.Dept, &row.JobType, &row.Level, &row.CostCenter, &row.FatherName, &row.EmergencyNo, &row.ConfirmationPeriod, &row.PFDate, &row.CTC, &row.FBP, &row.VarPay, &row.AdminBank, &row.EmpBank, &row.IFSC, &row.PayMode, &row.RepManager, &row.RepManEmail, &row.MaritalStatus, &row.SalaryTemp, &row.IsRepMan, &row.SpouseName, &row.Relation, &row.IsMetro, &row.HasESS, &row.HasPF, &row.PFUAN, &row.IsEPFEntitled, &row.HasESI, &row.IsPFRestricted, &row.BankAcNo, &row.Designation, &row.DeptCode, &row.Role, &row.JobTitle, &row.CustomField, &row.OnboardingStatus); err != nil {
log.Fatal(err)
}
models.AllUniversal = append(models.AllUniversal, row)
} else {
if err := rows.Scan(&row.AdhaarNo, &row.Gender, &row.Branch, &row.EmpName, &row.DOB, &row.DOJ, &row.Gender, &row.LocAdd1, &row.LocAdd2, &row.LocCity, &row.LocState, &row.LocCountry, &row.PerAdd1, &row.PerAdd2, &row.PerCity, &row.PerState, &row.PerCountry, &row.OfficeMail, &row.OfficeMobile, &row.LocPIN, &row.PerPIN, &row.MotherName, &row.ESINo, &row.PAN, &row.VisaNo, &row.VisaExpiry, &row.IntegreationRef, &row.OrgID, &row.DeptID, &row.SectionID, &row.JobTypeID, &row.GradeID, &row.DesignationID, &row.CustomGroup1ID, &row.CustomGroup2ID, &row.CustomGroup3ID, &row.Card1, &row.Card2, &row.ScheduleGroupID, &row.StartShift, &row.PersonalEmail, &row.WeakOffGropuID, &row.LeaveGroup, &row.Field1, &row.PFNo, &row.BloodGroup, &row.SerialNo, &row.PassportNo, &row.PassportExpiry, &row.Dept, &row.JobType, &row.Level, &row.CostCenter, &row.FatherName, &row.EmergencyNo, &row.ConfirmationPeriod, &row.PFDate, &row.CTC, &row.FBP, &row.VarPay, &row.AdminBank, &row.EmpBank, &row.IFSC, &row.PayMode, &row.RepManager, &row.RepManEmail, &row.MaritalStatus, &row.SalaryTemp, &row.IsRepMan, &row.SpouseName, &row.Relation, &row.IsMetro, &row.HasESS, &row.HasPF, &row.PFUAN, &row.IsEPFEntitled, &row.HasESI, &row.IsPFRestricted, &row.BankAcNo, &row.Designation, &row.DeptCode, &row.Role, &row.JobTitle, &row.CustomField, &row.OnboardingStatus); err != nil {
log.Fatal(err)
}
models.AllUniversal = append(models.AllUniversal, row)
}
}
return models.AllUniversal
}