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:

Database Schema

1 of 419 articles shown, currently no other people reading this forum.
photo
From: Timothy F.
Date: Wed 13th May, 1:16pm
Actions: 
        Login-to-reply
ANONYMOUS wrote:

> Hey Tim,
> 
> I'm trying to implement such schema so that Question can have many Options and 
> correct_option_id can only have 1 correct answer. But this gave me an error:
> 
> "Could not determine join condition between parent/child tables on relationship 
> Question.options - there are multiple foreign key paths linking the tables.  Specify the 
> 'foreign_keys' argument, providing a list of those columns which should be counted as 
> containing a foreign key reference to the parent table."
> 
> Do I need many-to-many relationship?
> 
> class Question(db.Model):
>     __tablename__ = 'question'
>     id = db.Column(db.Integer, primary_key=True)
>     set_id = db.Column(db.Integer, db.ForeignKey('set.id'))
>     question_text = db.Column(db.Text)
>     correct_option_id = db.Column(db.Integer, db.ForeignKey('option.id'))
>     
>     options = db.relationship('Option', backref='question', lazy='dynamic')
> class Option(db.Model):
>     __tablename__ = 'option'
>     id = db.Column(db.Integer, primary_key=True)
>     question_id = db.Column(db.Integer, db.ForeignKey('question.id'))
>     option_text = db.Column(db.Text)

The db.relationship normally automtically determines how to join two tables, but in this case it finds it ambiguous 
(probably because question.correct_option_id references option.id, and  option.question_id references question.id).

To remove the ambiguity you can use the foreign keys field:
options = db.relationship('Option', backref='question', foreign_keys='Option.question_id', lazy='dynamic')

Related articles

Database Schema (all 6) RSS
├─ original   Tue 5th May, 3:35pm, ANONYMOUS
├─ reply 1   Wed 6th May, 10:59am, Takoyaki N.
├─ reply 2   Wed 6th May, 12:13pm, Timothy F.
├─ reply 3   Mon 11th May, 10:02am, ANONYMOUS
├─ reply 4   Wed 13th May, 12:37pm, Takoyaki N.
└─ THIS   Wed 13th May, 1:16pm, Timothy F.
This Page


Program written by: [email protected]
Feedback welcome
Last modified:  8:27am May 24 2020