r/flask • • May 13 '23

Ask r/Flask Sqlalchemy help

I’m currently teaching myself sqlalchemy and I’ve made a website where users can log on and create a video game library using the igdb api. I’ve created a database that stores all the “user” data, and I have a separate database to store the “game” data. At this point all the users share the same “game” database. I’d like each user to only be able to access their own game library but I can’t figure out how to do it or what to search to learn how to do it. Can anyone point me in the right direction?

3 Upvotes

8 comments sorted by

5

u/Rangerdth May 13 '23

You want all users to share the same game database (so you don't duplicate data).

What you need is another table of games "owned" by user, which relates to the game database. It'll be a many-to-many relationship, so you'll need an association table between users and games.

See this video by PrettyPrinted (which is not me, it's just informative).

2

u/Hak_Saw5000 May 13 '23

Thank you so much for your help. This video is great!

1

u/Rangerdth May 14 '23

You’re welcome!

1

u/Hak_Saw5000 May 14 '23

I have one more question if you don't mind. If I want to associate a game to a user in my webpage, would I do it the same way? As in using '.append()'?
I can't figure out how to access the currently logged in user.

3

u/beardedperuvian May 14 '23

One way to do that is to have a user tied to a session. And when you do your fetch for the games make sure that the session function when a user is logged in is included in your fetch.

1

u/Rangerdth May 14 '23

Yes. As /u/beardedperuvian wrote, you’ll need to capture the user ID when they log in and create a session, otherwise everyone will get the same info.

2

u/Benseddik_Oussama May 13 '23

suppose u have a table named Game where u store the games u should add an other table called user_can_access_game in the database where u store games with these columns:

id_user id_game

where the id_game is foreign key to game_table and the composition <id_user,id_game> is the primary key for this table

0

u/EnvironmentalOil7910 May 13 '23

I am not really from web background so if my answer is not viable I Apologise for the same.What I can say is that you should always jot down what your aim is and figure out which SQL type is best meaning nosql or SQL,and that is hard sometimes.So if possible you can use any sort of say a unique I'd...uuid associated with each of the users and then pass this uuid to all other routes as well based on which you can query the game they have only created? You can also do JWT if not uuid and pass it along,try to search how to send jwt tokens and jwt_required it will help you perhaps,also while each user logs in you need to store that unique value and reference it as foreign key in game table for that specific user then only it may work.