qizhiyi / sqlalchemy-migrate

Automatically exported from code.google.com/p/sqlalchemy-migrate
MIT License
0 stars 0 forks source link

Column.create is getting assigned to table being affected on. #116

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
Code below does not work.

from sqlalchemy import *
from migrate import *

meta = MetaData()

disciplines_table = Table('stage_disciplines', meta)
tours_table = Table('tours', meta)
starts_table = Table('starts', meta)

def upgrade(migrate_engine):
    meta.bind = migrate_engine
    type_col = Column('type', String)
    type_col.create(disciplines_table)
    type_col.create(tours_table)
    type_col.create(starts_table)

def downgrade(migrate_engine):
    meta.bind = migrate_engine
    type_col = Column('type', String)
    type_col.drop(disciplines_table)
    type_col.drop(tours_table)
    type_col.drop(starts_table)

It fails with error:

agladilin@skyogre-xubuntu:server$ python manage.py migrate test
Upgrading...
Traceback (most recent call last):
  File "manage.py", line 37, in <module>
    migrate_main(url=settings.DB_URL, debug='False', repository='db')
  File "/usr/local/lib/python2.6/dist-packages/sqlalchemy_migrate-0.6-py2.6.egg/migrate/versioning/shell.py", line 203, in main
    ret = command_func(**kwargs)
  File "<string>", line 2, in test
  File "/usr/local/lib/python2.6/dist-packages/sqlalchemy_migrate-0.6-py2.6.egg/migrate/versioning/util/__init__.py", line 160, in with_engine
    return f(*a, **kw)
  File "/usr/local/lib/python2.6/dist-packages/sqlalchemy_migrate-0.6-py2.6.egg/migrate/versioning/api.py", line 218, in test
    script.run(engine, 1)
  File "/usr/local/lib/python2.6/dist-packages/sqlalchemy_migrate-0.6-py2.6.egg/migrate/versioning/script/py.py", line 140, in run
    script_func(engine)
  File "db/versions/002_add_types.py", line 14, in upgrade
    type_col.create(tours_table)
  File "/usr/local/lib/python2.6/dist-packages/sqlalchemy_migrate-0.6-py2.6.egg/migrate/changeset/schema.py", line 526, in create
    engine._run_visitor(visitorcallable, self, connection, **kwargs)
  File "/usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.6.6-py2.6.egg/sqlalchemy/engine/base.py", line 1740, in _run_visitor
    **kwargs).traverse_single(element)
  File "/usr/local/lib/python2.6/dist-packages/sqlalchemy_migrate-0.6-py2.6.egg/migrate/changeset/ansisql.py", line 55, in traverse_single
    ret = super(AlterTableVisitor, self).traverse_single(elem)
  File "/usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.6.6-py2.6.egg/sqlalchemy/sql/visitors.py", line 83, in traverse_single
    return meth(obj, **kw)
  File "/usr/local/lib/python2.6/dist-packages/sqlalchemy_migrate-0.6-py2.6.egg/migrate/changeset/ansisql.py", line 103, in visit_column
    self.execute()
  File "/usr/local/lib/python2.6/dist-packages/sqlalchemy_migrate-0.6-py2.6.egg/migrate/changeset/ansisql.py", line 44, in execute
    return self.connection.execute(self.buffer.getvalue())
  File "/usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.6.6-py2.6.egg/sqlalchemy/engine/base.py", line 1191, in execute
    params)
  File "/usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.6.6-py2.6.egg/sqlalchemy/engine/base.py", line 1287, in _execute_text
    return self.__execute_context(context)
  File "/usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.6.6-py2.6.egg/sqlalchemy/engine/base.py", line 1302, in __execute_context
    context.parameters[0], context=context)
  File "/usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.6.6-py2.6.egg/sqlalchemy/engine/base.py", line 1401, in _cursor_execute
    context)
  File "/usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.6.6-py2.6.egg/sqlalchemy/engine/base.py", line 1394, in _cursor_execute
    context)
  File "/usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.6.6-py2.6.egg/sqlalchemy/engine/default.py", line 299, in do_execute
    cursor.execute(statement, parameters)
sqlalchemy.exc.ProgrammingError: (ProgrammingError) column "type" of relation 
"stage_disciplines" already exists
 '\nALTER TABLE stage_disciplines ADD type VARCHAR' {}

As you can see the filure happens on the second alter. It means, 
sqlalchemy-migrate added column to stage_disciplines and the tries to add the 
same column to the same table. Although there is another table specified.

I would expect that my code could work. I have worked around the issue with 
following code:

def upgrade(migrate_engine):
    meta.bind = migrate_engine
    type_col = Column('type', String)
    type_col.create(disciplines_table)
    type_col = Column('type', String)
    type_col.create(tours_table)
    type_col = Column('type', String)
    type_col.create(starts_table)

This works fine.

What version of the product are you using? On what operating system?

linux, xubuntu-10.10 inside virtualbox
python-2.6
sqlalchemy_migrate-0.6-py2.6.egg
SQLAlchemy-0.6.6-py2.6.egg

Please provide any additional information below.

Original issue reported on code.google.com by andrey.g...@gmail.com on 4 Mar 2011 at 7:22

GoogleCodeExporter commented 8 years ago
I see no easy way to fix this issue. Documenting the workaround would be a good 
idea.

Original comment by jan.ditt...@gmail.com on 28 Oct 2011 at 12:31

GoogleCodeExporter commented 8 years ago

Original comment by jan.ditt...@gmail.com on 28 Oct 2011 at 1:56