honeysql

danielneal 2025-10-21T15:08:07.432869Z

When using next.jdbc / honeysql to do a query joining a table to itself (e.g. :from [:device :d] :join [[:device :parent] [:= :device.parent_id :device.id]]) - is there a way of using the alias as the namespace (- so we get device/... and parent/...) , as opposed to the table name? Db is Mysql

p-himik 2025-10-21T15:14:46.659569Z

Yes. It's all about formatting by HoneySQL so the actual DB is irrelevant. By default, both :a.b and :a/b will be formatted as a.b.

danielneal 2025-10-21T15:16:11.070839Z

Oh - I mean in the results - so that the result set as qualified maps has device/ and parent/

danielneal 2025-10-21T15:16:38.536929Z

rather then device/ with the second device/ clobbering the first? Or is that what you’ve answered, but I’ve not understood quite?

p-himik 2025-10-21T15:18:26.821619Z

Ah, I see. No clue then, I don't use that functionality myself.

danielneal 2025-10-21T15:20:00.016669Z

Thanks anyway!

danielneal 2025-10-21T15:28:16.067029Z

Ah not sure if this is a bad idea, but it looks like I can accomplish it by using a :with ... CTE and putting the alias in there

seancorfield 2025-10-21T15:53:27.501619Z

Just to be clear: HoneySQL does not run SQL, it only generates it. next.jdbc is what runs the SQL and it uses the table name as reported by the JDBC driver under the hood. next.jdbc has no control over that, so it's the JDBC driver returning the underlying table name for both device.* and for your aliased parent.*. What you can do in that situation is alias the columns you are selecting from parent so they are identifiable as distinct from the same columns selected from device.

seancorfield 2025-10-21T15:53:43.502029Z

(I think the docs even specifically talk about that case...)

danielneal 2025-10-21T15:54:43.672969Z

Thanks Sean for the clarification - is there anything wrong with using the with CTE to alias the table - this does seem to get picked up by JDBC

seancorfield 2025-10-21T15:54:47.087889Z

Yeah, see https://cljdoc.org/d/com.github.seancorfield/next.jdbc/1.3.1070/doc/getting-started#options--result-set-builders and the caveats about aliases.

seancorfield 2025-10-21T15:55:27.075079Z

No idea about with -- I don't use CTEs because I'm on an older MySQL version.

seancorfield 2025-10-21T15:56:32.326429Z

Like I said above, the qualifier in the keywords comes from whatever the JDBC driver reports as the table name, so if that works for you, fine. You'll have to make the call on using a CTE from a SQL p.o.v. but it has no impact on next.jdbc or HoneySQL...

danielneal 2025-10-21T15:57:43.498729Z

Cool, thanks. Yeah it seems to work, for some reason I suppose the alias on the CTE causes getTableName to return that name rather than the source table - which makes sense. I guess I’ll see what my colleagues think.

p-himik 2025-10-21T15:58:38.232339Z

CTEs could result in a different execution plan. It's not necessarily bad, just something to be aware of.

👍 1