SELECT_QUERY = `SELECT * FROM events WHERE c_id = ? AND start_time > ? and
                                    end_time < ?`

query := sr.db.Raw(SELECT_QUERY, request.GetCId(), startTime, endTime)

    var v = request.GetVIds()
    if len(v) > 0 {
        query = query.Where(` v_id IN (?) `, v)
    } //Only this block introduces first ) after end_time

var c = request.GetStatus().String()
    if len(c) > 0 {
        query = query.Where( " status = ? ", c) // this introduces the other opening brace //after AND
    }

以下是生成并在日志中找到的查询
 SELECT * FROM events WHERE c_id = 1 AND start_time > '2020-04-16 18:42:00' and
                                        end_time < '2020-04-16 18:45:50' ) AND ( v_id IN (1,2)) AND ( status = 'STATUS_MIDDLE_CLASS'  ORDER BY  start_time DESC  LIMIT 5 OFFSET 1

stackoverflow和Internet文章中的其他解决方案没有帮助。

PS:是因为我混合了db.Raw()和query.Where()吗?

最佳答案

改变了吗?至$ 1不能解决问题。

基本上有几件事解决了这个问题。

1)混合Rawquery.Where是一个问题。

在对Raw进行查询之后

2)

SELECT_QUERY = `SELECT * FROM events WHERE c_id = ? AND start_time > ? and
                                    end_time < ?`

已经有sr.db.Where。然后使用select * from引入嵌套的query := sr.db.Raw(SELECT_QUERY, request.GetCId(), startTime, endTime)
因此,如下更改SELECT_QUERY
SELECT_QUERY = `events WHERE c_id = ? AND start_time > ? and
                                    end_time < ?`

解决了这个问题。

关于postgresql - 为什么gorm postgresql在“)”处或附近抛出pq:语法错误?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/61342328/

10-16 14:16