How do I change a sqlalchemy model to reflect a migration done using alembic?

266 views Asked by At

I have a model defined using sqlalchemy, something like:

class Recommendation(Base):
    """ An entry is created by each recommender per URL """
    __tablename__ = 'recommendation'

    id = Column(Integer, primary_key=True)
    url_id = Column(Integer, ForeignKey('url.id'))

I realized recently that I (foolishly) forgot to add the 'score' parameter there, but the system is in production. I updated my definition to include a score then:

class Recommendation(Base):
    """ An entry is created by each recommender per URL """
    __tablename__ = 'recommendation'

    id = Column(Integer, primary_key=True)
    url_id = Column(Integer, ForeignKey('url.id'))
    score = Column(Float, default=0.0)

Changing the model works fine for my behave tests (yay for full coverage) because I destroy and recreate the database with each test. Problem is, in production, the score column doesn't exist, so just adding 'score' to the model doesn't work. My current plan is to use alembic to migrate the database to having a score column, but in my test environment, everything breaks because alembic tries to add a column that already exists. However, if I use alembic, then my original model file hasn't been updated with the score column, and my tests don't pass and the code doesn't work, but the score column is created in the test database.

So how do I add the 'score' column to the model that's used by the rest of my code only once it's been added to the db?

0

There are 0 answers