Skip to content

Instantly share code, notes, and snippets.

@mazz
Created June 7, 2017 20:33
Show Gist options
  • Save mazz/7d63e521316859f4ae852e5cea5d84eb to your computer and use it in GitHub Desktop.
Save mazz/7d63e521316859f4ae852e5cea5d84eb to your computer and use it in GitHub Desktop.
Migration fails when changing to single table inheritance
class MediaBase(Base):
#: The table in the database
__tablename__ = "mediabase"
#: Database primary key for the row (running counter)
id = Column(Integer, autoincrement=True, primary_key=True)
# table inheritance
media_type = Column(String(32), nullable=False)
#: Publicly exposed non-guessable
uuid = Column(UUID(as_uuid=True), default=uuid4)
localizedname = Column(Unicode(128), default=None)
#: url
url = Column(Unicode(384), default=None)
# full iso language-locale identifier i.e. zh-Hans-US
language_id = Column(String(16), default=None)
# name of the person presenting the material
presenter_name = Column(Unicode(64), default=None)
source_material = Column(Unicode(128), default=None)
# table inheritance
__mapper_args__ = {'polymorphic_on': media_type}
def __repr__(self):
"""Shell and debugger presentation."""
return '{} ({}) {} <{}>'.format(self.localizedname, self.language_id, str(self.uuid), self.url)
def __str__(self):
"""Python default and admin UI string presentation."""
return '{} ({}) presenter: {} source: {} <{}>'.format(self.localizedname, self.language_id, self.presenter_name, self.source_material, self.url)
class MediaChapter(MediaBase):
#: The table in the database
__tablename__ = "mediachapter"
__mapper_args__ = {'polymorphic_identity': 'chapter'}
id = Column(Integer, ForeignKey('mediabase.id'), primary_key=True)
#: Which chapter this media is part of
chapter_id = Column(Integer, ForeignKey('chapter.id'))
chapter = relationship("Chapter", back_populates="mediachapter")
@mazz
Copy link
Author

mazz commented Jun 7, 2017

class Chapter(Base):

    #: The table in the database
    __tablename__ = "chapter"

    #: Database primary key for the row (running counter)
    id = Column(Integer, autoincrement=True, primary_key=True)
    absolute_id = Column(Integer)

    #: Publicly exposed non-guessable id
    uuid = Column(UUID(as_uuid=True), default=uuid4)
    basename = Column(Unicode(64), default=None)

    book_id = Column(Integer, ForeignKey('book.id'))
    book = relationship("Book", back_populates="chapters")

    #: Relationship mapping between mediachapter and chapter.
    #: Each mediachapter can have only one chapter.
    #: Deleting chapter deletes its mediachapter.
    mediachapter = relationship("MediaChapter",
                           back_populates="chapter",
                           lazy="dynamic",
                           cascade="all, delete-orphan",
                           single_parent=True)

    def __repr__(self):
        """Shell and debugger presentation."""
        return "{} {}".format(self.book.basename, self.id)

    def __str__(self):
        """Python default and admin UI string presentation."""
        if hasattr(self.book, 'basename'):
            return "{} {}".format(self.book.basename, self.absolute_id)
        else:
            return "{} {}".format('(not set)', self.absolute_id)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment