2

I am currently working with Flask, Flask-SQLAchlemy and Flask-Restless to provide endpoints for an AngularJS backend application. I have set up the necessary many to many relationships between tables thanks to another user.

I have a new issue, which is that I have to set the count column in my junction table. In addition, I also have to increment count by 1. I have been looking through the Flask Restless Docs to find a solution, but I haven't seen any reference to accessing a junction table column. I have also tried accessing it through include columns without success. Here are my involved tables:

surveyOptions = db.Table('survey_options',
    db.Column('survey_id', db.Integer, db.ForeignKey('survey.id')),
    db.Column('option_id', db.Integer, db.ForeignKey('option.id')),
    db.Column('count', db.Integer)    # increment this value                 
    )

class Survey(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    category = db.Column(db.String(50))
    question = db.Column(db.Text)
    startDate = db.Column(db.DateTime)
    endDate = db.Column(db.DateTime)
    options = db.relationship('Option', secondary=surveyOptions,
        backref=db.backref('surveys', lazy='dynamic'), lazy='dynamic')

class Option(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    text = db.Column(db.Text)

Any guidance on how to increment the surveyOptions.count column would be greatly appreciated. I am more than willing to provide more code samples if you need any.

Edit

count will be incremented everytime that a user selects one of the options, to keep track of how many people prefer each option

DFenstermacher
  • 564
  • 1
  • 9
  • 23
  • When does `count` get incremented? – dirn Nov 03 '14 at 12:46
  • count will be incremented everytime someone clicks on that option. So I can keep track of when how many people select each option – DFenstermacher Nov 03 '14 at 14:36
  • I think you'd be better off adding `survey_id` and `count` to `Option`. Then you wouldn't need the join table at all. – dirn Nov 03 '14 at 14:48
  • 1
    At the moment you're using an association table to handle the many to many association. If you're storing extra information in that, you need to use an association object instead. Also-- are you sure you need a M2M table? One survey has many choices, but does one choice really have many surveys? – Doobeh Nov 03 '14 at 15:42

0 Answers0