Issue: create_tables(...,safe=True) fails after migration unless Meta.db_table is defined on model
Author: aurcioli-handy
State: closed
Created: 2017-11-15 18:32:53

**Expected behavior:**
create_tables([MyModel], safe=True) does not raise an error if table exists

**Observed behavior:**
`peewee.InternalError: (1050, "Table 'MyModel' already exists")`

**How to reproduce**
1. Create a simple model definition and run create_tables()
2. Create and run a migration to add a column to the table
3. Run create_tables(...,safe=True)

**Problem**
`table_exists()` method checks if cls._meta.db_table is contained in cls._meta.database.get_tables`, however cls._meta.db_table returns an all lowercase string name, while cls._meta.database.get_tables respects the case on the model.

```python
mymodel in ['MyModel']
```
fails as expected

**Fix:**
To mitigate this issue i've added the following code to my model

```python
class MyModel(BaseModel):
    foo = TextField()
    class Meta:
        db_table = 'MyModel'
```


I'm confused... Peewee normalizes the case of the table name from the model name in the absence of an explicit `db_table`, so the first call to create_tables() in step 1 should be using the same table name as in step 3.

This is odd, it appears to be changing the case unpredictably.

Here is what I am seeing:

this is my model (note i am forcing it to downcase on this example)
At each step I will show the output of `cls._meta.db_table` and `cls._meta.database.get_tables`

### 1. Initial create_tables (table does not yet exist)
```python
class KubernetesUser(BaseModel):
    ldap_username = TextField()
    token = TextField()
    uid = TextField()
    created_at = DateTimeField()

    class Meta:
        db_table = 'kubernetesuser'
```

I run `create_tables([KubernetesUser], safe=True)`
Table created successfully:
`cls._meta.db_table` = kubernetesuser
`cls._meta.database.get_tables` = []

### 2. Run create_tables again to show output and verify its working as expected
```python
create_tables([KubernetesUser], safe=True)
```

`cls._meta.db_table` = kubernetesuser
`cls._meta.database.get_tables` = ['kubernetesuser']

### 3. Run a migration to add a column
```python
migrate(
            migrator.add_column('KubernetesUser', 'token_updated_at', token_updated_at_field)
    )
```
Column is successfully added

### 4. Run create_tables again
```python
create_tables([KubernetesUser], safe=True)
```

`cls._meta.db_table` = kubernetesuser
`cls._meta.database.get_tables` = ['KubernetesUser']

***pymysql.err.InternalError: (1050, "Table 'kubernetesuser' already exists")***

Is it the migration causing the table name to be changed?



Yep that seems to be it - the migration is changing the case and the code is always expecting lowercase.. 

before migration:
mysql> show tables;
+--------------------------+
| Tables_in_kube_ldap_auth |
+--------------------------+
| kubernetesuser           |
+--------------------------+

after migration:
+--------------------------+
| Tables_in_kube_ldap_auth |
+--------------------------+
| KubernetesUser           |
+--------------------------+

seems its my fault then for adding case to the table name in the migration.

Knowing that peewee normalizes the name to lowercase, perhaps the migration code should force lowercase on the table name for migrations to avoid users shooting themselves in the foot like I have.

Yeah in your migration you need to specify the table name using the same case as the model name. "kubernetesuser". That ought to fix ya.