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


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

load_user() for users in different tables

1 of 419 articles shown, currently no other people reading this forum.
From: Timothy F.
Date: Sun 3rd May 2020, 11:50am


"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.  

Related articles

load_user() for users in different tables (both) RSS
├─ original   Fri 1st May 2020, 10:52pm, Mingchuan T.
└─ THIS   Sun 3rd May 2020, 11:50am, Timothy F.
This Page

Program written by: [email protected]
Feedback welcome
Last modified: 11:27am Sep 21 2020