2

Here is my code :

package main

import (
    "fmt"
    "github.com/jmoiron/sqlx"
    _ "github.com/lib/pq"
    "log"
    "time"
)

type Commune struct {
    Id int `db:"id"`
    Created time.Time `db:"created"`
    Modified time.Time `db:"modified"`
    Name string `db:"name"`
}

func main() {
    var err error
    db, err = sqlx.Connect("postgres", "user=toto 
    dbname=tata password=titi sslmode=disable")
    commune := []Commune{}
    db.Select(&commune, `SELECT * FROM "Geo_commune"  WHERE id=1  ORDER BY name ASC`)
    rows, err2 := db.Query(`SELECT * FROM "Geo_commune"  WHERE id=1  ORDER BY name ASC`)
    fmt.Println(commune)
    fmt.Println(rows)
    if err != nil {
    log.Fatalln(err)
    }
     if err2 != nil {
    log.Fatalln(err2)
    }

   }

here is the data in the database :

enter image description here

edit : the table_name :

enter image description here

and i've even tried with :

db.Select(&commune, `SELECT * FROM "Geo_commune"  WHERE id=1  ORDER BY name ASC`)

but the return is always empty and i'am sure that the data exists and i don't have a connection error.

Without the "Geo_commune" rows, err2 := db.Query(SELECT * FROM Geo_commune WHERE id=1 ORDER BY name ASC) i have a : 2019/01/28 22:17:16 pq: relation "geo_commune" does not exist

Regards

edit my new tests :

package main

import (
    "fmt"
    "github.com/jmoiron/sqlx"
    "log"
    "time"
    _ "github.com/lib/pq"
)
type Commune struct {
    Id int `db:"id"`
    Created time.Time `db:"created"`
    Modified time.Time `db:"modified"`
    Name string `db:"name"`

}
var db *sqlx.DB
func main() {
    var err error
    db, err = sqlx.Connect("postgres", "user=toto dbname=titi password=tata sslmode=disable")
    commune := []Commune{}

    if err != nil {
        log.Fatalln(err)
    }
    db.Select(&commune, `SELECT * FROM "Geo_commune"  WHERE id=1  ORDER BY name ASC`)
    rows, err2 := db.Query(`SELECT * FROM "Geo_commune"  WHERE id=1  ORDER BY name ASC`)
    if err != nil {
        log.Fatalln(err)
    }
    if err2 != nil {
        log.Fatalln(err2)
    }


    db.Select(&commune, `SELECT * FROM public.Geo_commune  WHERE id=1  ORDER BY name ASC`)
    rows, err2 = db.Query(`SELECT * FROM public.Geo_commune  WHERE id=1  ORDER BY name ASC`)
    fmt.Println(commune)
    fmt.Println(rows)
    if err != nil {
        log.Fatalln(err)
    }
    if err2 != nil {
        log.Fatalln(err2)
    }



    }

gives me :

[]
<nil>
2019/01/29 09:50:06 pq: relation "public.geo_commune" does not exist
7
  • 4
    Don't ignore the error returned by Select. Commented Jan 28, 2019 at 21:12
  • @Peteri've edited and no error .... Commented Jan 28, 2019 at 21:16
  • 2
    Your table seems to have much more columns than your struct has fields, wouldn't that be a problem when using SELECT * ...? Did you try a listing the four columns that have corresponding fields in the Commune struct? This test seems to suggest that Select should return an error if you're missing a "destination" field, and in the code you provided you're not checking that specific error, since the result of your db.Select call isn't assigned to anything. Commented Jan 28, 2019 at 23:47
  • 1
    You're still not checking the error returned from db.Select, why? And what's the purpose of db.Query in your code? Use only one, not both. Commented Jan 29, 2019 at 9:08
  • 1
    @mpkopriva you're right it's that thanks , i miss some fields in my struct and i didn't truly checked the error of select Commented Jan 29, 2019 at 10:56

1 Answer 1

1

I'm confused with your description. did it throw pq: relation "geo_commune" does not exist? if it did, your datasource is incorrect. if it didn't, try select * from public.Geo_commune

Sign up to request clarification or add additional context in comments.

1 Comment

i have a 2019/01/29 09:29:13 pq: relation "public.geo_commune" does not exist error because my table is Geo_commune

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.