Peewee select with circular dependency <p>I have two models <code>Room</code> and <code>User</code>. Every user is assigned to exactly one room and one of them is the owner.</p> <pre><code>DeferredUser = DeferredRelation() class Room(Model): owner = ForeignKeyField(DeferredUser) class User(Model): sessid = CharField() room = ForeignKeyField(Room, related_name='users') DeferredUser.set_model(User) </code></pre> <p>Now, having <code>sessid</code> of the owner I'd like to select his room and all assigned users. But when I do:</p> <pre><code>(Room.select(Room, User) .join(User, on=Room.owner) .where(User.sessid==sessid) .switch(Room) .join(User, on=User.room)) </code></pre> <p>It evaluates to:</p> <pre><code>SELECT "t1".*, "t2".* # skipped column names FROM "room" AS t1 INNER JOIN "user" AS t2 ON ("t1"."owner_id" = "t2"."id") INNER JOIN "user" AS t2 ON ("t1"."id" = "t2"."room_id") WHERE ("t2"."sessid" = ?) [&lt;sessid&gt;] </code></pre> <p>and throws <code>peewee.OperationalError: ambiguous column name: t2.id</code> as <code>t2</code> is defined twice.<br> What I actually need to do is:</p> <pre><code>room = (Room.select(Room) .join(User.sessid=sessid) .get()) users = room.users.execute() </code></pre> <p>But this is N+1 query and I'd like to resolve it in a single query like:</p> <pre><code>SELECT t1.*, t3.* FROM room AS t1 INNER JOIN user AS t2 ON t1.owner_id = t2.id INNER JOIN user as t3 ON t3.room_id = t1.id WHERE t2.sessid = ?; </code></pre> <p>Is there a peewee way of doing this or I need to enter this SQL query by hand?</p>