Search code examples
mysqljoinaliaspeewee

Avoiding conflicting column titles in table join in peewee


I'm trying to join two tables in peewee with a mysql database. This is pretty easy doing something like this:

s = Table1.select(Table1, Table2).join(
        Table2).naive().where(Table1.Title == "whatever")

Unfortunately, I have called a column in Table1 and Table2 the same thing, "URL". Then when I select s.URL it gives me the URL from Table2, which I don't want, I want the one from Table1. Is there some way to either not join the Table2.URL column or to name it something different? This question seems to be addressing a similar problem in regular SQL (not peewee), is there a way to do something similar in peewee?

In other words, I think I'm looking for either a "JOIN AS" method or a "DON'T JOIN THIS COLUMN" method in peewee.

Thanks a lot, Alex


Solution

  • I haven't used peewee, but the docs suggest that any table, expression, or column has a .alias() method. But that means you'd have to alias the URL column individually:

    Table1.select(Table1, Table2.URL.alias('t2_url')).join(Table2)...
    

    Even in hand-crafted SQL, you can't SELECT Table2.* but simultaneously give an alias for one of the columns of Table2.