Faculty of Engineering and Mathematical Sciences 
Not logged in (login)

help3403


This forum is provided to promote discussion amongst students enrolled in Agile Web Development (CITS3403).
 
Options:
RSS cloud
Jump to:

load_user() for users in different tables

2 of 419 articles shown, currently no other people reading this forum.
photo
From: Mingchuan T.
Date: Fri 1st May, 10:52pm
Actions: 
        Login-to-reply

 

Hi,

load_user() is a key function for flask_login to work. However, the return 
statement "return User.query.get(int(user_id))" seems to only return a user from 
User table. 

What if there are two tables that store different types of users (eg. 
teachers/students)? Or, should all users be stored in the same table for it to 
work?


Thanks

load_user() for users in different tables

photo
From: Timothy F.
Date: Sun 3rd May, 11:50am
Actions: 
        Login-to-reply

 

"Mingchuan Tian" <22*3*5*[email protected]*u*e*t*u*a*e*u*a*> wrote:

> Hi,
> 
> load_user() is a key function for flask_login to work. However, the return 
> statement "return User.query.get(int(user_id))" seems to only return a user from 
> User table. 
> 
> What if there are two tables that store different types of users (eg. 
> teachers/students)? Or, should all users be stored in the same table for it to 
> work?
> 
> 
> Thanks

Good question.

See https://flask-login.readthedocs.io/en/latest/

The requirement for load_user is that it returns an object that satisfies certain 
properties (or inherits from UserMixin). There is no requirement that all 
possiblke returned objects come from the same table. You may take them from 
several tables, However, as load_user takes a single user_id, you should make sure 
that the ids in the two tables do not overlap.

There are probably better patterns you can use for this problem. For example, in 
your teacher/student example, you could have a Users table (used for login) and 
then a teachers table and a Students table, both with foriegn keys linkng to the 
Users table. This is how inheritence is implemented in relational databases.
Note that this will be different to using python inheritence with SQL-Alchemy 
models, which I wouldn't recommend.  
This Page


Program written by: [email protected]
Feedback welcome
Last modified:  3:57pm Aug 06 2020