Issue: TypeError: sequence item 0: expected str instance, BaseModel found
Author: raphael0202
State: closed
Created: 2017-02-13 10:27:21

This error can be reproduced with the following script:
```
import peewee
import datetime

db = peewee.SqliteDatabase(database='test.db')


class BaseModel(peewee.Model):
    class Meta:
        database = db


class User(BaseModel):
    username = peewee.CharField(max_length=200, null=True)


class Bot(BaseModel):
    name = peewee.CharField(max_length=30, unique=True, index=True)


class Message(BaseModel):
    user = peewee.ForeignKeyField(User, related_name='messages', null=True)
    bot = peewee.ForeignKeyField(Bot, related_name='messages', null=True)
    pub_datetime = peewee.DateTimeField()

    @classmethod
    def date_filter(cls, bot_id, user_id,
                    target_datetime=datetime.datetime.now()):
        start_datetime = target_datetime.replace(hour=0, minute=0)
        end_datetime = start_datetime.replace(day=start_datetime.day + 1)
        return (Message.select(User, Message, Bot)
                       .join(User, Bot)
                       .where(User.id == user_id,
                              Bot.id == bot_id,
                              Message.pub_datetime.between(
                                  start_datetime,
                                  end_datetime)))


if __name__ == '__main__':
    db.create_tables([Bot, Message, User])
    bot = Bot.create(name='test')
    user = User.create(username='username')
    Message.create(bot=bot, user=user, pub_datetime=datetime.datetime.now())
    messages = Message.date_filter(bot.id, user.id)
    print(messages)
```

with the following traceback:
```
Traceback (most recent call last):
  File "/home/raphael/git_core/likeabot/test_db.py", line 47, in <module>
    print(messages)
  File "/home/raphael/.virtualenvs/likeabot/lib/python3.5/site-packages/peewee.py", line 2690, in __repr__
    sql, params = self.sql()
  File "/home/raphael/.virtualenvs/likeabot/lib/python3.5/site-packages/peewee.py", line 3117, in sql
    return self.compiler().generate_select(self)
  File "/home/raphael/.virtualenvs/likeabot/lib/python3.5/site-packages/peewee.py", line 1951, in generate_select
    return self.build_query(clauses, alias_map)
  File "/home/raphael/.virtualenvs/likeabot/lib/python3.5/site-packages/peewee.py", line 1835, in build_query
    return self.parse_node(Clause(*clauses), alias_map)
  File "/home/raphael/.virtualenvs/likeabot/lib/python3.5/site-packages/peewee.py", line 1788, in parse_node
    sql, params, unknown = self._parse(node, alias_map, conv)
  File "/home/raphael/.virtualenvs/likeabot/lib/python3.5/site-packages/peewee.py", line 1754, in _parse
    sql, params = self._parse_map[node_type](node, alias_map, conv)
  File "/home/raphael/.virtualenvs/likeabot/lib/python3.5/site-packages/peewee.py", line 1682, in _parse_clause
    node.nodes, alias_map, conv, node.glue)
  File "/home/raphael/.virtualenvs/likeabot/lib/python3.5/site-packages/peewee.py", line 1813, in parse_node_list
    node_sql, node_params = self.parse_node(node, alias_map, conv)
  File "/home/raphael/.virtualenvs/likeabot/lib/python3.5/site-packages/peewee.py", line 1788, in parse_node
    sql, params, unknown = self._parse(node, alias_map, conv)
  File "/home/raphael/.virtualenvs/likeabot/lib/python3.5/site-packages/peewee.py", line 1754, in _parse
    sql, params = self._parse_map[node_type](node, alias_map, conv)
  File "/home/raphael/.virtualenvs/likeabot/lib/python3.5/site-packages/peewee.py", line 1682, in _parse_clause
    node.nodes, alias_map, conv, node.glue)
  File "/home/raphael/.virtualenvs/likeabot/lib/python3.5/site-packages/peewee.py", line 1816, in parse_node_list
    return glue.join(sql), params
TypeError: sequence item 0: expected str instance, BaseModel found
```

The error is gone when deleting the Message.user foreign key, and the User references in where and join clauses.

Environment:
- Python 3.5
- Peewee 2.8.5


`.join(User, Bot)` is not valid. The docs are pretty clear on the method: http://docs.peewee-orm.com/en/latest/peewee/api.html#Query.join

Interestingly, since you're joining the same model twice, you'll need to work around Peewee's concept of the query context. Here's what you're probably going for:

```python
        return (Message.select(User, Message, Bot)
                       .join(User)
                       .switch(Message)  # Query context was User, since we want to join message->bot, we switch back.
                       .join(Bot)
                       .where(User.id == user_id,
                              Bot.id == bot_id,
                              Message.pub_datetime.between(
                                  start_datetime,
                                  end_datetime)))
```

Oh my bad, I missed this point in the doc. Thank you for the code fix.
I think however that it would be great if the error message were more user-friendly.