librelist archives

« back to archive

Flask-SQLAlchemy: Can't query related fielsd

Flask-SQLAlchemy: Can't query related fielsd

From:
Denis Gaydukov
Date:
2014-01-19 @ 12:33
Hi everyone, I can't understand why my query does not work:
My model:

class Order(db.Model):
    __tablename__ = 'orders'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    user_id = db.Column(db.Integer, db.ForeignKey('users.id'))
    user = db.relationship('User', backref='order', lazy='joined')
    title = db.Column(db.String(30))
    subject_id = db.Column(db.Integer, db.ForeignKey('subjects.id'))
    subject = db.relationship('Subject', backref='order', lazy='joined')
    category_id  = db.Column(db.Integer, db.ForeignKey('categories.id'))
    category = db.relationship('Category', backref='order', lazy='joined')
    os_id = db.Column(db.Integer, db.ForeignKey('os.id'))
    os = db.relationship('OS', backref='order', lazy='joined')
    deadline = db.Column(db.String(16))
    details = db.Column(db.String(2048))
    explanations = db.Column(db.Boolean())
    submit_date = db.Column(db.DateTime, default=datetime.now())


My routine:
@app.route('/profile', methods=['GET'])
def profile():
    if 'username' not in session:
        return redirect(url_for('login'))

    orders = Order.query.\
        with_entities(Order.id, Order.title, Order.subject, Order.category,
Order.deadline).\
        filter_by(user_id=int(session['user_id'])).\
        all()

    return render_template('profile.html', orders=orders)


I'm getting a big list of orders and in the fields category and subject
only True and False values

When I'm using:
orders = Order.query.filter_by(user_id=int(session['user_id'])).all()

everything is working fine