Need help with the FlaskFM project

I am stuck adding data to my DB. This is step 13. When I run:

python3 add_data.py

I get error:

NoForeignKeysError: Can’t find foreign key relationships between ‘playlist’ and ‘item’.

I have checked and double checked the relationship and foreignkey entries in my code and everything checks out perfectly. Here is my code:

app.py models.py and routes.py

Hi,

I’m not finding anywhere where you have a playlist with a lower-case p defined. Was this intentional? I think that might be the error.

items = db.relationship('Item', backref = 'playlist', lazy='dynamic')

playlist_id = db.Column(db.Integer, db.ForeignKey('playlist.id'))

I was following the instructions which says:

class One(db.Model):
    id = db.Column(db.Integer, primary_key = True)
    many = db.relationship('One', backref=’owner’, lazy='dynamic')

class Many(db.Model):
    id = db.Column(db.Integer, primary_key = True)
    one = db.Column(db.Integer, db.ForeignKey('one.id'))

Add a new column to the Playlist model similar to the many column in One, but replace many with items, One with Item, and backref = 'owner' with backref = 'playlist'.

I have followed the instructions exactly which you can see in my code. I tried to change ‘playlist’ with ‘Playlist’ but get the same error. I’ve now gone through the instructions at least 15 times and my code perfectly matches the instructions. Here is what the error looks like:

sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such column: item.playlist_id
[SQL: SELECT item.id AS item_id, item.song_id AS item_song_id, item.playlist_id AS item_playlist_id
FROM item
WHERE ? = item.playlist_id]

It could be a typo. Or it’s referencing something that’s not in sight (like a table name or variable that was declared with a lowercase). The flask module is new and is known to have some inaccuracies so I’m not 100% sure about the accuracy of the example (but since I can’t see the whole context, I don’t want to pre-judge).

@course8845053547 There’s one point I do wonder about: is Playlist supposed to have a playlist_id foreignkey pointing to itself?

Regardless, I recommend going over the sqlalchemy documentation on this stuff. It’s a little dense, but then again, databases are no walk in the park. They do differentiate the case in their examples but it’s very clear why and how it’s linked. https://docs.sqlalchemy.org/en/13/orm/basic_relationships.html

I can’t speak to the strength of the CC examples because I never did these modules (though they seem really fun). Miguel Grinberg has great (free) material on sqlalchemy within flask, both in blog form and youtube. Corey Schaffer is not bad either. There’s an sqlalchemy book by O’Reilly that really goes into detail for all this stuff but that might be a bit overkill unless you plan to do a lot with sqlalchemy.

The sqlalchemy reddit is another place that might have useful information. Although you have to ask permission to post and they’re a little slow in maintaining it.

1 Like

So I just had an epiphany a minute ago! I was feeling that the problem I was having had something to do with the issues I had in creating and populating the database. When I completed that step in the instructions for some reason I was getting errors in importing the db elements. Back when I was doing that step I did some checking and found a typo and tried again and found no errors. I thought that was it.

Fast forward to the issue I asked for help on. I was thinking and thinking about it and I felt like there was something deeper in the issue I was having, and it wasn’t the code I was writing. Besides, I retyped and rechecked the code, went to the documentation on the web and learned how everything worked, and came back to the project and checked again. I couldn’t find a typo or issue with my code. I now know more about how Flask works with SQLAlchemy than I probably ever wanted to know! Lets call that a good side effect.

Anyways, I decided to delete the db in the file system, re-create the db and populate it again. This time I got no errors and things went perfectly. I saved the project and then presto! The links that were supposed to appear on the website were there. All of the buttons worked and I was able to complete the project!

I guess this is the process. If you get an error keep plugging away until it works. Go on out on the internet and read the docs if you don’t have the answer. Ask for help from others. It is the final aspect I would like to thank you for! I was just going to skip to the end of the project and move on. It took me a couple of days but I feel I am better off for plugging away, and ultimately finding a solution. Thank you for being there! I see your name around in the topics so I’m sure you are there a lot, helping all of us newbies figure things out. Someday, once I’m an old hat programmer, I’ll repay my debt and help out the new people by answering questions. Pass it along, right? Take care!

Ah so that was it!

I should’ve recommended to check in the flask shell if normal queries were working, which is what I normally do before I even start troubleshooting.

Regardless it’s good you kept digging. Grit is the x-factor with these things. With enough persistence over time a ton of obstacles can be overcome.

See you around!

1 Like

Hey,
this project was really fun.
However I am stuck with step 23. My songs are not updated to the database.

I assume my code in routes.py row 71ff is not correct. I tried different alternatives, but could not get it to function. Any ideas on why the song is not added to the db?

App.Py

Due to new user restrictions I am only allowed to add two links to a post :slight_smile:
Cheers,
D

You can always just give a link to the github folder where all the flask files are (just for reference)