sqlalchemy.orm.exc.UnmappedInstanceError

时间:2014-12-17 08:37:12

标签: sqlalchemy pyramid

每当我尝试在金字塔应用程序中删除sqlalchemy中的grand-child项时,我都会收到此错误

UnmappedInstanceError: Class 'sqlalchemy.ext.declarative.api.DeclarativeMeta' is not mapped; was a class (beatstore.models.Song) supplied where an instance was required?

以下是我在视图和模型中的删除代码

模型

class Song(Base):
    __tablename__ = 'songs'
    id = Column('song_id', Integer, primary_key=True)
    # foreing key
    # nullable = false, the song must have an artist
    artist_id = Column(Integer, ForeignKey('artists.artist_id', onupdate='CASCADE', ondelete='CASCADE'), nullable=False)
    artist = relationship("Artist")
    # foreing key
    album_id = Column(Integer, ForeignKey('albums.album_id', onupdate='CASCADE', ondelete='CASCADE'), nullable=False)
    album = relationship("Album")
    # foreing key
    genre_id = Column(Integer, ForeignKey('genres.genre_id', onupdate='CASCADE', ondelete='CASCADE' ))
    genre = relationship("Genre")
    picture_path = image_attachment('PictureSong')
    title = Column(Unicode(100), nullable=False)
    download_link = Column(Unicode(100), nullable=False)
    artist = Column(Unicode(100), nullable=False)
    duration = Column(Unicode(50))
    Price = Column(Unicode(50))
    created = Column(DateTime, default=datetime.now ,  nullable=False)

浏览

@view_config(route_name="song_delete")
def song_delete(song, request):
    """song delete """
    id = request.matchdict['id']
    dbsession = DBSession()

    song = dbsession.query(Song).filter_by(id = id).first()
    if song is None:
        request.session.flash("error;Song not found!")
        return HTTPFound(location=request.route_url("media"))        

    try:
        transaction.begin()
        dbsession.delete(Song);
        transaction.commit()
        request.session.flash("warning;The song is deleted!")
    except IntegrityError:
        # delete error
        transaction.abort()
        request.session.flash("error;The song could not be deleted!")

    return HTTPFound(location=request.route_url("media"))

1 个答案:

答案 0 :(得分:1)

您正在尝试删除班级名称:
dbsession.delete(Song)

而不是对象:
dbsession.delete(song)(注意小写 s