Answer the question
In order to leave comments, you need to log in
Golang gorm querying a boolean from a database, not a table field, is that possible?
Hello. I use GORM to work with a Postgres database. In the psql console, I can simply query (does an origin column exist in the items table with such and such a value):
SELECT EXISTS( SELECT 1 FROM items WHERE origin = 'http://example.com/path' );
and get response:?column?
----------
t
(1 row)
var thd bool
x := mygormdb.Raw(`SELECT EXISTS(
SELECT 1 FROM items WHERE origin = 'http://www.infokam.su/n17724.html' )
as isfieldexists;`).Row()
x.Scan(&thd)
fmt.Println("Result:", thd)
Answer the question
In order to leave comments, you need to log in
On pure database/sql:
r, err := db.Query("SELECT 1 FROM items WHERE origin='http://example.com/path' LIMIT 1")
if err != nil {
// Oops!
}
thisTableReallyHasThatRow := r.Next()
r.Close()
thisTableReallyHasThatRow := !db.Find(&Item{}, "origin = ?", "http://example.com/path").RecordNotFound()
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question