Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

187
Visualizações
go-pg UnionAll - limit whole expression

I am trying to use .UnionAll method in go-pg library for golang.

    var model []Customer
    q0 := db.Model(&model).Where("name = ?", name0).Limit(4)
    q1 := db.Model(&model).Where("name = ?", name1).Limit(3)
    var result []Customer
    if err := q0.UnionAll(q1).Limit(1).Select(&result); !as.NoError(err) {
        return
    }

This code produces the query:

(SELECT "customer"."id", "customer"."name" FROM customers AS "customer" WHERE (name = 'customer 1deificatory zonoid reprepare alacrify serenissime') 
LIMIT 1) 
UNION ALL 
(SELECT "customer"."id", "customer"."name" FROM customers AS "customer" WHERE (name = 'customer 2fordless ferroboron radiability dandizette smutch'
LIMIT 3)
)

But I expect it to be:

(SELECT "customer"."id", "customer"."name" FROM customers AS "customer" WHERE (name = 'customer 1deificatory zonoid reprepare alacrify serenissime') 
LIMIT 4) 
UNION ALL 
(SELECT "customer"."id", "customer"."name" FROM customers AS "customer" WHERE (name = 'customer 2fordless ferroboron radiability dandizette smutch')
LIMIT 3)

LIMIT 1

So how to use go-pg to get raw SQL query which I expect? The thing is I can't apply the Limit 1 expression to the whole query for some reason. Also, my Limit 4 is not applied correctly to the first union all member. My whole code is here.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

I believe this query does the trick for you.

if err := db.Model().With("union_q", q0.UnionAll(q1)).Table("union_q").Limit(1).Select(&result); !as.NoError(err) {
    return
}

The query isn't exactly what you wanted to be and as far as I know, the current version of go-pg can't generate that query. But this query does exactly what you wanted.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda