Flask sqlalchemy InterfaceError

时间:2014-02-11 14:05:02

标签: python sqlalchemy flask jinja2

我不确定这里出了什么问题......我收到了这个错误:

InterfaceError: (InterfaceError) Error binding parameter 0 - probably unsupported type. u'SELECT contact.id AS contact_id, contact.surname AS contact_surname, contact.firstname AS contact_firstname, contact.email AS contact_email, contact.mobile AS contact_mobile,  contact.work_location AS contact_work_location \nFROM contact \nWHERE contact.id = ?' ([1],)

我的方法:

@app.route('/contacts/<int:contact_id>', methods=['GET'])
def contact_detail(contact_id):
    if request.method == 'GET':
        db.session.query(Contact).filter_by(id=[contact_id]).all()
        return render_template('modcontact.html', title = 'Contact Detail')

我的模特:

class Contact(db.Model):
id = db.Column(db.Integer, primary_key = True)
surname = db.Column(db.String(100))
firstname = db.Column(db.String(100))
email = db.Column(db.String(100))
mobile = db.Column(db.String(20))
work_location = db.Column(db.String(100))
#user_id = db.Column(db.Integer, db.ForeignKey('user.id'))

def __repr__(self):
    return '<Contact %r>' % (self.surname)

模板:

{% extends "base.html" %}

{% block content %}

 <h1>List of contacts</h1>
 <ul class=contacts>
{% for contacts in contacts %}
<li><h3>
  <a href="{{ url_for('contact_detail',contact_id=contacts.id)}}">    
  {{ contacts.surname }}, {{ contacts.firstname }}
  </a>
</h3></li>
 {% else %}
   <li><em>No contacts available</em></li>
 {% endfor %}
 </ul>
 <a href="/addcontact/">Add a new contact</a>

{% endblock %}

1 个答案:

答案 0 :(得分:3)

您在查询过滤器中传递了一个列表。因此查询中的参数是一个列表,因此是'错误绑定参数0'。

请改为:db.session.query(Contact).filter_by(id=contact_id).all()