zio-sql
zio-sql copied to clipboard
Render unique names for database tables
Consider a following self-referencing schema:
CREATE TABLE items (
id INTEGER NOT NULL PRIMARY KEY,
name VARCHAR NOT NULL,
parent_id INTEGER,
CONSTRAINT fk_parent_id FOREIGN KEY (parent_id) REFERENCES items(id)
)
it's possible to create a query like this:
val table = columnSet.table("items")
val id :*: nm :*: parentId :*: _ = table.columns
val id2 :*: nm2 :*: _ :*: _ = table.columns
val selfJoined = select(id ++ nm ++ parentId ++ nm2) from(table join table).on(id2 === parentId)
that will render as:
select items.id, items.name, items.parent_id, items.name from items inner join items on items.id = items.parent_id
This is incorrect. Instead the PostgresModule#renderRead
should generate aliases for the tables so that the query would look something like this:
select i1.id, i1.name, i1.parent_id, i2.name from items as i1 inner join items as i2 on i2.id = i1.parent_id;
I can pick this one
@jczuchnowski while working on this ticket, I should support only postgresql ? and mysql/oracle later ?