Issue: Regression on migrator.drop_column
Author: stenci
State: closed
Created: 2014-12-09 16:23:32

The following code works with 2.3.3, fails with 2.4.4.

```
import peewee
import playhouse.migrate
db = peewee.SqliteDatabase('test.db', threadlocals=True)

class MyTable(peewee.Model):
    mycolumn = peewee.CharField()
    class Meta:
        database = db

MyTable.drop_table(True)
MyTable.create_table()

migrator = playhouse.migrate.SqliteMigrator(db)
playhouse.migrate.migrate(migrator.drop_column('mytable2', 'mycolumn'))
```

Here is the traceback:

```
Traceback (most recent call last):
  File "F:\workspace\test\test.py", line 17, in <module>
    playhouse.migrate.migrate(migrator.drop_column('mytable2', 'mycolumn'))
  File "C:\Python34\lib\site-packages\playhouse\migrate.py", line 535, in migrate
    operation.run()
  File "C:\Python34\lib\site-packages\playhouse\migrate.py", line 144, in run
    getattr(self.migrator, self.method)(*self.args, **kwargs))
  File "C:\Python34\lib\site-packages\playhouse\migrate.py", line 135, in _handle_result
    result.run()
  File "C:\Python34\lib\site-packages\playhouse\migrate.py", line 144, in run
    getattr(self.migrator, self.method)(*self.args, **kwargs))
  File "C:\Python34\lib\site-packages\playhouse\migrate.py", line 138, in _handle_result
    self._handle_result(item)
  File "C:\Python34\lib\site-packages\playhouse\migrate.py", line 133, in _handle_result
    self.execute(result)
  File "C:\Python34\lib\site-packages\playhouse\migrate.py", line 129, in execute
    self.migrator.database.execute_sql(sql, params)
  File "C:\Python34\lib\site-packages\peewee.py", line 2780, in execute_sql
    cursor.execute(sql, params or ())
ValueError: operation parameter must be str
```


That's odd, I copy/pasted the code into a module locally and it ran without issue, although I did modify the code slightly so the call to `drop_column()` referred to `mytable` and not `mytable2`. I also explicitly checked out the 2.4.4 tag.

Can you do some debugging to find out what values are being passed to `cursor.execute()`?


Sorry, I didn't notice that `2`, which in my case referred to an old existing table and in your case referred to a non existing table.

My old table had a primary key and an indexed column, and it looks like that's what changes the behavior between the two versions.

The following code reproduces the problem starting from an empty database.

```
import peewee
import playhouse.migrate
db = peewee.SqliteDatabase('test.db', threadlocals=True)

class MyTable(peewee.Model):
    pk = peewee.CharField(primary_key=True)
    mycolumn = peewee.CharField(index=True)
    class Meta:
        database = db

def create_table():
    MyTable.drop_table(True)
    MyTable.create_table()

migrator = playhouse.migrate.SqliteMigrator(db)

create_table()
try:    playhouse.migrate.migrate(migrator.drop_column('mytable', 'mycolumn'))
except: print('1) error with good db and good column')

create_table()
try:    playhouse.migrate.migrate(migrator.drop_column('notable', 'mycolumn'))
except: print('2) error with bad db and good column')

create_table()
try:    playhouse.migrate.migrate(migrator.drop_column('mytable', 'nocolumn'))
except: print('3) error with good db and bad column')

create_table()
try:    playhouse.migrate.migrate(migrator.drop_column('notable', 'nocolumn'))
except: print('4) error with bad db and bad column')
```

Output with 2.3.3:

```
2) error with bad db and good column
4) error with bad db and bad column
```

Output with 2.4.4:

```
1) error with good db and good column
2) error with bad db and good column
3) error with good db and bad column
4) error with bad db and bad column
```

The traceback on the first error is:

```
Traceback (most recent call last):
  File "F:\eclipse\plugins\org.python.pydev_2.8.2.2013090511\pysrc\pydevd_comm.py", line 773, in doIt
    result = pydevd_vars.evaluateExpression(self.thread_id, self.frame_id, self.expression, self.doExec)
  File "F:\eclipse\plugins\org.python.pydev_2.8.2.2013090511\pysrc\pydevd_vars.py", line 376, in evaluateExpression
    result = eval(compiled, updated_globals, frame.f_locals)
  File "<string>", line 1, in <module>
  File "C:\Python34\lib\site-packages\playhouse\migrate.py", line 535, in migrate
    operation.run()
  File "C:\Python34\lib\site-packages\playhouse\migrate.py", line 144, in run
    getattr(self.migrator, self.method)(*self.args, **kwargs))
  File "C:\Python34\lib\site-packages\playhouse\migrate.py", line 135, in _handle_result
    result.run()
  File "C:\Python34\lib\site-packages\playhouse\migrate.py", line 144, in run
    getattr(self.migrator, self.method)(*self.args, **kwargs))
  File "C:\Python34\lib\site-packages\playhouse\migrate.py", line 138, in _handle_result
    self._handle_result(item)
  File "C:\Python34\lib\site-packages\playhouse\migrate.py", line 133, in _handle_result
    self.execute(result)
  File "C:\Python34\lib\site-packages\playhouse\migrate.py", line 129, in execute
    self.migrator.database.execute_sql(sql, params)
  File "C:\Python34\lib\site-packages\peewee.py", line 2780, in execute_sql
    cursor.execute(sql, params or ())
ValueError: operation parameter must be str
```

The `cursor.execute()` on line 2780 is executed with the following parameters:

```
sql, params
('select name, sql from sqlite_master where type=? and LOWER(name)=?', ['table', 'mytable'])
```

then the execution goes to the `else` block and crashes at the `self.commit()` on line 2788. (I don't understand why the 2780 is reported in the traceback)


Because you have a non-integer primary key, sqlite will automatically create an index. The index is found when introspecting the db, but it has no associated SQL, so we're unable to re-create it (which is fine, because SQLite will re-create it anyways).


The issue is not an issue for me. I reported because often a difference between two versions is a red flag. 
It's up to you to close it or fix it (assuming that there is something to fix)

Thank you!!!!!!


> It's up to you to close it or fix it (assuming that there is something to fix)

I did fix it. In b0c866c