bokwoon95/sq

cannot call JSONField for static queries

recht opened this issue · 6 comments

Latest release fails for us with cannot call JSONField for static queries. The query is using a mapper that looks something like this:

func mapper(r *sq.Row) json.RawMessage {
	var pref json.RawMessage
	r.JSONField(&pref, table.Value)
	return pref
}

Oh sorry about that, can you post the full invocation to Fetch/FetchAll? Static queries were a recent addition and apply to queries which explicitly specify the fields to be fetched instead of leaving it up to the rowmapper.

For query builders

  • sq.Select(a,b,c).From(d).Where(e) would be considered static (the rowmapper can only reference fields a, b and c)
  • sq.From(d).Where(e) would be considered dynamic (fields depend on what the rowmapper invokes)

But it should only affect static queries, so if a dynamic query is showing "cannot call XXXX for static queries" that would be considered a bug.

Hi, sorry for the late reply. As far as I can tell, it's not allowed to call any of the r.*Field if it's a static query, you have to reference by name? That seems like a strange limitation given that the query can be created via sq.Select(someField)?

It's an unfortunate side effect of the implementation (yeah I know, it's also a breaking change as you found out ☹). May I ask, why not just omit Select(someField) so that it becomes a dynamic query? That way you can use the r.*Field methods as usual.

Initially we were not aware of the dynamic nature, so out of old habit I just enumerated all the necessary columns. However, it also turns out that not all mappers are fully safe to run on null values. For example, reading a uuid using r.UUIDField fails, so we have something like


	func() {
		defer func() {
			_ = recover()
		}()

		r.UUIDField(&info.UserID, usersTable.ID)
	}()

to bypass that. Similarly, we'd like to do json queries in postgres, like sq.Stmt("content->description").As("description"), which also fails in the mapper because it generates a query with select description from...

oh god thanks for bringing that up to my attention, I didn't realize UUIDField did not handle NULLs.

UUIDField should handle NULLs correctly now, if you update to v0.5.1.

Similarly, we'd like to do json queries in postgres, like sq.Stmt("content->description").As("description"), which also fails in the mapper because it generates a query with select description from...

Can you show me a snippet of code that is generating select description from... instead of select content->description as description from...? It should not be happening.