flaskflask-sqlalchemyweb-development-server

InvalidRequestError: Entity namespace "album" has no property "album_name"


I am making a music streaming app using flask and flask-sqlalchemy. I created a form to upload a song along with the album name. I first checked whether the album is new to add the album first into the database:

@app.route('/upload-a-song',methods=['GET','POST'])
def upload_song():
    form=SongForm()
    creator=Creator.query.filter_by(username=session['username'])
    if form.validate_on_submit():
        album=Album.query.filter_by(album_name=form.album_name.data).first()
        if not album:
            new_album=Album(album_name=form.album_name.data,creator=session['username'])
            db.session.add(new_album)
            new_song=Songs(title=form.song_title.data,lyrics=form.lyrics.data,
                        genre=form.genre.data,album_id=new_album.album_id)
            db.session.add(new_song)
            db.session.commit()
            flash('Song added successfully')
            return redirect(url_for('user_home',user_name=new_album.creator))
        else:
            new_song=Songs(title=form.song_title.data,lyrics=form.lyrics.data,
                        genre=form.genre.data,album_id=album.album_id)
            db.session.add(new_song)
            db.session.commit()
            flash('Song added successfully')
            return redirect(url_for('user_home',user_name=album.creator))
    if form.errors!={}:
        for err_msg in form.errors.values():
            flash(f'There was an error in uploading the song: {err_msg}',category='error')
    if request.method=='GET':
        return render_template('upload_song.html',form=form,creator=creator)

And here is my models:

class Album(db.Model):
    __tablename__='album'
    __searchable__=['ablum_name']
    album_id=db.Column(db.Integer(),nullable=False,primary_key=True,unique=True)
    ablum_name=db.Column(db.String(50),nullable=False,unique=True)
    release_date=db.Column(db.Date(),nullable=False,default=date.today())
    creator_id=db.Column(db.String(),db.ForeignKey('creator.creator_id'))
    song=db.relationship('Songs',back_populates='album')


#for all the songs
class Songs(db.Model):
    __tablename__='songs'
    __searchable__=['title']
    song_id=db.Column(db.Integer(),nullable=False,primary_key=True,unique=True)
    title=db.Column(db.String(50),nullable=False,unique=True)
    lyrics=db.Column(db.String(50000),nullable=False)
    genre=db.Column(db.String(30))
    album_id=db.Column(db.String(),db.ForeignKey('album.album_id'))
    album=db.relationship('Album',back_populates='song')

But as I upload the song from the form, I get this error:

sqlalchemy.exc.InvalidRequestError: Entity namespace for "album" has no property "album_name"

Please help!

I tried changing the column name but the error persisted


Solution

  • Hey did you notice that you have a Typo?

    You have defined the column as ablum_name instead of album_name.

    If you change the model name to:

    album_name=db.Column(db.String(50),nullable=False,unique=True)
    

    It should work. So in that case the error message gave you a good hint on where to look.