zio / zio-quill

Compile-time Language Integrated Queries for Scala
https://zio.dev/zio-quill
Apache License 2.0
2.15k stars 346 forks source link

Reserved words like "to" should be aliased for PostrgeSQL #1710

Open b-gyula opened 4 years ago

b-gyula commented 4 years ago

Version: 3.4.10 Module: quill-jdbc??? Database: PostrgeSQL

Expected behavior

Reserved words like "order" or "to" shall be aliased when generating SQL regardless the naming strategy.

Actual behavior

Using "to" or any reserved word as name reference generates invalid SQL for PostgreSQL. E.g

for { order <- query[Ordr] to <- query[TradeOrdr].leftJoin(to => to.ordr == order.id) } yield (order)

Generates:

SELECT order.id, order.volume FROM ordr order LEFT JOIN trade_ordr to ON to.ordr_id = order.id

Both the "order" and "to" shall be aliased. Like

SELECT x1.id, x1.volume FROM ordr x1 LEFT JOIN trade_ordr x2 ON x2.ordr_id = x1.id

or

SELECT "order".id, "order".volume FROM ordr "order" LEFT JOIN trade_ordr "to" ON "to".ordr_id = "order".id

See this scastie as an example.

Might be related to Decouple identifier escaping from NamingStrategy

@getquill/maintainers

juliano commented 4 years ago

@b-gyula have you tried the NamingStrategy PostgresEscape?

val ctx = new SqlMirrorContext(PostgresDialect, PostgresEscape)