Issue: MySQL: peewee adds extra characters to table name when db.create_tables()
Author: Eric-hjy
State: closed
Created: 2016-05-11 06:54:05

As the title, when I used peewee to create mysql tables, some characters were added by peewee and that made table creation fail. To make sure it was not my python codes making the error, I printed out
the table names before calling db.create_tables()

Below are the logs of my codes plus the reported error traces:

Connect to DB: AFA_INF_TATA            // this is the traces of my codes
Instance BAIDU_INF_MOLA Start        // this is the traces of my codes
Log: Create Database table dfd-df-4-345d-67-init-done_other-tata_all_stats_table, dfd-df-4-345d-67-init-done_other-mola_all_host_table           //these are printed table names

Traceback (most recent call last):
  File "./mon_query.py", line 496, in <module>
    mon_query_func()
  File "./mon_query.py", line 474, in mon_query_func
    db.create_tables([MonQueryTable, ServiceHostTable], safe = True)
  File "/usr/lib/python2.6/site-packages/peewee-2.6.3-py2.6.egg/peewee.py", line 3386, in create_tables
    create_model_tables(models, fail_silently=safe)
  File "/usr/lib/python2.6/site-packages/peewee-2.6.3-py2.6.egg/peewee.py", line 4565, in create_model_tables
    m.create_table(*_create_table_kwargs)
  File "/usr/lib/python2.6/site-packages/peewee-2.6.3-py2.6.egg/peewee.py", line 4309, in create_table
    cls._create_indexes()
  File "/usr/lib/python2.6/site-packages/peewee-2.6.3-py2.6.egg/peewee.py", line 4333, in _create_indexes
    db.create_index(cls, fields, unique)
  File "/usr/lib/python2.6/site-packages/peewee-2.6.3-py2.6.egg/peewee.py", line 3396, in create_index
    return self.execute_sql(_qc.create_index(model_class, fobjs, unique))
  File "/usr/lib/python2.6/site-packages/peewee-2.6.3-py2.6.egg/peewee.py", line 3304, in execute_sql
    self.commit()
  File "/usr/lib/python2.6/site-packages/peewee-2.6.3-py2.6.egg/peewee.py", line 3152, in **exit**
    reraise(new_type, new_type(*exc_args), traceback)
  File "/usr/lib/python2.6/site-packages/peewee-2.6.3-py2.6.egg/peewee.py", line 3296, in execute_sql
    cursor.execute(sql, params or ())
  File "/usr/lib64/python2.6/site-packages/MySQL_python-1.2.3c1-py2.6-linux-x86_64.egg/MySQLdb/cursors.py", line 173, in execute
    self.errorhandler(self, exc, value)
  File "/usr/lib64/python2.6/site-packages/MySQL_python-1.2.3c1-py2.6-linux-x86_64.egg/MySQLdb/connections.py", line 36, in defaulterrorhandler
    raise errorclass, errorvalue
peewee.OperationalError: (1059, "Identifier name 'dfd-df-4-345d-67-init-done_other-mola_all_host_table_fb51e9a09dcfdac685fc80af91c194d7' is too long")

From error traces, I saw the table name is changed to "dfd-df-4-345d-67-init-done_other-mola_all_host_table_fb51e9a09dcfdac685fc80af91c194d7"

Below is the related python codes:

```
    statsTableName = get_query_table_name(service_name)
    hostTableName = get_service_host_table_name(service_name)
    LogFunc(log_f, 1, 'Create Database table ' + statsTableName + ', ' + hostTableName + '\n')
    QueryTable._meta.db_table = statsTableName
    ServiceHostTable._meta.db_table = hostTableName
    db.create_tables([QueryTable, HostTable], safe = True)
```