Search code examples
sqlalchemyalembic

Alembic Migration: How to remove polymorphic Identity


I have the following setup

class Content(Base):
    """Content object"""
    __tablename__ = "content"
    id = Column(Integer, primary_key=True)
    name = Column(Unicode(255),unique=True, nullable=False)
    title = Column(Unicode(255))
    body = Column(UnicodeText)
    created = Column(DateTime, default=func.now())
    modified = Column(DateTime, onupdate=func.now())
    type = Column(String(20))
    __mapper_args__ = {
    'polymorphic_on':type,
    'polymorphic_identity':'content',
    'with_polymorphic':'*'
}


class Locality(Content):
    __tablename__ = "local"
    id = Column(Integer, ForeignKey('content.id'),primary_key=True)

    city_name = Column(Unicode(80))
    __mapper_args__ = {'polymorphic_identity':'city'}

Now I dropped the Locality table using alembic. Each time I query Content, I get

AssertionError: No such polymorphic_identity 'city' is defined

How do I drop this polymorphic_identity


Solution

  • I got over this by applying MySQL 'delete from' command on the content through MySQL Console, finding those contents whose type is 'city'

    delete from content where content.type='city';