zio-sql icon indicating copy to clipboard operation
zio-sql copied to clipboard

Render unique names for database tables

Open jczuchnowski opened this issue 4 years ago • 2 comments

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;

jczuchnowski avatar Nov 11 '20 21:11 jczuchnowski

I can pick this one

maciejbak85 avatar Dec 16 '20 08:12 maciejbak85

@jczuchnowski while working on this ticket, I should support only postgresql ? and mysql/oracle later ?

maciejbak85 avatar Jan 04 '21 13:01 maciejbak85