Reputation: 704
I keep getting this error whenever i try deleting the grand-child item in sqlalchemy in pyramid application
UnmappedInstanceError: Class 'sqlalchemy.ext.declarative.api.DeclarativeMeta' is not mapped; was a class (beatstore.models.Song) supplied where an instance was required?
Here is my Delete code in both views and models
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"))
Upvotes: 0
Views: 2980
Reputation: 17735
You are trying to delete the class name:
dbsession.delete(Song)
instead of the object:
dbsession.delete(song)
(note lowercase s)
Upvotes: 1