Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cannot call JSONField for static queries #14

Open
recht opened this issue May 15, 2024 · 6 comments
Open

cannot call JSONField for static queries #14

recht opened this issue May 15, 2024 · 6 comments

Comments

@recht
Copy link
Contributor

recht commented May 15, 2024

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
}
@bokwoon95
Copy link
Owner

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.

@recht
Copy link
Contributor Author

recht commented Aug 28, 2024

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)?

@bokwoon95
Copy link
Owner

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.

@recht
Copy link
Contributor Author

recht commented Aug 29, 2024

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...

@bokwoon95
Copy link
Owner

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

@bokwoon95
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants