DSC-McMaster-U / Gamified-Learning-Platform

MIT License
6 stars 14 forks source link

Internal server error due to faulty models.py database relationships #72

Closed alexchen2 closed 8 months ago

alexchen2 commented 10 months ago

When starting up the Flask app, the app crashes and throws an Internal Server Error to the page. The terminal output shows:

sqlalchemy.exc.InvalidRequestError: One or more mappers failed to initialize - can't proceed with initialization 
of other mappers. Triggering mapper: 'Mapper[Course(course)]'. Original exception was: Error creating backref 
'courses' on relationship 'Course.users': property of that name exists on mapper 'Mapper[User(user)]'

Based off the error message, this probably has to do with something being duplicated within whatever relationship the Course and User tables have within the models.py database model (I haven't taken a look yet at the tables in question at the time of posting, but just wanted to put this out there first to notify of a potential crash when attempting to run the current project).

alexchen2 commented 10 months ago

Commenting out the duplicated columns mentioned above (lines 88 to 90 of models.py) as a temporary workaround leads to another error (on line 23 of main.py) when trying to access the quiz page:

sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such column: quiz.topic_id
[SQL: SELECT quiz.id AS quiz_id, activity.id AS activity_id, activity.title AS activity_title, activity.subject_type AS activity_subject_type, activity.start_time 
AS activity_start_time, activity.end_time AS activity_end_time, activity.activity_type AS activity_activity_type, quiz.active AS quiz_active, quiz.level AS quiz_level, quiz.score AS quiz_score, quiz.topic_id AS quiz_topic_id
FROM activity JOIN quiz ON activity.id = quiz.id
WHERE activity.id = ?]
[parameters: (0,)]
(Background on this error at: https://sqlalche.me/e/20/e3q8)

To reproduce this error, register and login to any dummy account, then change the route to "/quiz/[any number here]".

zareentk commented 10 months ago

Setting this issue to @clarawong20 and @angadk4 since they are the most familiar with the database system for this section of the application! If you guys could look into the cause and diagnose some potential solutions as well as documenting the cause/origin I think it would be a great learning experience for the entire team! Please let me know if you need any support I can always try and help out if this becomes a major blocker

angadk4 commented 10 months ago

In terms of the original error I believe that there is an issue with the back ref naming. Essentially, at the moment it is attempting to create a back-reference named 'courses' in the User model, but a property with this name already exists due to the earlier definition. I was able to solve this problem on my system by simply adding enrolled_ to the existing names of backrefs in the users relationships of Course, Module, and Topic (lines 205, 213, 222). Furthermore, there is duplicated backref naming in the User model (lines 88-90), which can be easily resolved by changing each backref name to enrolled_(courses/modules/topics)_users in order to make them unique. However, I am still encountering the quiz error. I am not too familiar with that part of the database, so maybe @clarawong20 could look over that part of the bug.