pythonflasksqlalchemyflask-sqlalchemysqlalchemy-migrate

Python/SQLAlchemy migration - ValueError: need more than 3 values to unpack when migrating


I'm following Miguel Grinberg's excellent Flask Mega-Tutorial and using his database creation and migration scripts (found here) but I'm running into a problem when changing a column in one of my models.

The old model was:

class Classes(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    day = db.Column(db.String(10))
    slot = db.Column(db.Integer)
    enrolments = db.relationship('Enrolment', backref='class_slot', lazy='dynamic')

    def __repr__(self):
        return '<Classes - %r slot %d>' % (self.day, self.slot)

class Enrolment(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    class_id = db.Column(db.Integer, db.ForeignKey('classes.id'))
    studentnum = db.Column(db.String(10), index=True, unique=True)
    name = db.Column(db.String(30))
    flags = db.Column(db.String(100))
    notes = db.Column(db.String(200))

    def __repr__(self):
        return '<Enrolment - (%r) %r>' % (self.name, self.studentnum)

with classes.day being a String representation. However I want to make this into a foreign key relationship so I can easily search and sort by day, so I added the following code to the model:

class Days(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    day_name = db.Column(db.String(20), unique=True)
    classes = db.relationship('Classes', backref='dayofweek', lazy='select')

    def __repr__(self):
        return '<Day - %r>' % self.day_name

and then modified the Classes model, changing the day column to:

day = db.Column(db.Integer, db.ForeignKey('days.id'))

When running the migration script, it chokes when generating the model with the following error:

aaron$ ./db_migrate.py 
Traceback (most recent call last):
  File "./db_migrate.py", line 12, in <module>
    script = api.make_update_script_for_model(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO, tmp_module.meta, db.metadata)
  File "<string>", line 2, in make_update_script_for_model
  File "/Users/aaron/Dropbox/Development/put-attendance/flask/lib/python2.7/site-packages/migrate/versioning/util/__init__.py", line 90, in catch_known_errors
    return f(*a, **kw)
  File "<string>", line 2, in make_update_script_for_model
  File "/Users/aaron/Dropbox/Development/put-attendance/flask/lib/python2.7/site-packages/migrate/versioning/util/__init__.py", line 160, in with_engine
    return f(*a, **kw)
  File "/Users/aaron/Dropbox/Development/put-attendance/flask/lib/python2.7/site-packages/migrate/versioning/api.py", line 321, in make_update_script_for_model
    engine, oldmodel, model, repository, **opts)
  File "/Users/aaron/Dropbox/Development/put-attendance/flask/lib/python2.7/site-packages/migrate/versioning/script/py.py", line 70, in make_update_script_for_model
    genmodel.ModelGenerator(diff,engine).genB2AMigration()
  File "/Users/aaron/Dropbox/Development/put-attendance/flask/lib/python2.7/site-packages/migrate/versioning/genmodel.py", line 223, in genB2AMigration
    for modelCol, databaseCol, modelDecl, databaseDecl in td.columns_different:
ValueError: need more than 3 values to unpack

Inspecting td.columns_different shows that it only has one item in the list - a string 'day'.

Why is it choking because I changed the type of field in the database?


Solution

  • Yes, td.columns_different is a dict of columns that changed between previous and current revision. This code belongs to sqlalchemy-migrate package.

    Comment from the source[1]:

    class TableDiff(object):
      """
      ...
    
      ..attribute:: columns_different
    
      A dictionary containing information about columns that were
      found to be different.
      It maps column names to a :class:`ColDiff` objects describing the
      differences found.
    

    For the solution to migrating your Column type from String to Integer see: How to alter column type from character varying to integer using sqlalchemy-migrate

    [1] https://github.com/stackforge/sqlalchemy-migrate/blob/75034abe515a49e0efafb91519cfdf448ba02c73/migrate/versioning/schemadiff.py#L121