'unicode' object has no attribute '_sa_instance_state'
<p>I'm trying to have a page where customers can select the side item and quantity of a given menu item and add it to their order. I'm running into trouble trying to submit it to my database...no matter what I try an AttributeError is thrown sayings whatever object has no attribute '_sa'...</p>

<p>Here's my models:</p>

<pre><code>class MenuItems(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(50), index=True, unique=True)
    category = db.Column(db.String(50))
    price = db.Column(db.Float(6))
    description = db.Column(db.String(140))
    image_path = db.Column(db.String(140), unique=True)
    def __unicode__(self):
        return self.name

class Orders(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    customer_id = db.Column(db.Integer, db.ForeignKey('customer.id'))
    created_on = db.Column(db.DateTime)
    submitted = db.Column(db.Boolean)
    submitted_on = db.Column(db.DateTime)
    order_items = db.relationship('OrderItems', backref='orders', lazy='dynamic')

    def __unicode__(self):
        return str(self.customer_id)

class OrderItems(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    order_id = db.Column(db.Integer, db.ForeignKey('orders.id'))
    menu_item_id = db.Column(db.Integer, db.ForeignKey('menu_items.id'))
    side_id = db.Column(db.Integer, db.ForeignKey('menu_items.id'))
    quantity = db.Column(db.Integer)

    menu_item = db.relationship(MenuItems, foreign_keys=[menu_item_id], 
                            backref="menu_item")
    side_item = db.relationship(MenuItems, foreign_keys=[side_id], 
                            backref="side_item")
    def __unicode__(self):
        return str(self.id)
</code></pre>

<p>And the form:</p>

<pre><code>class OrderForm(Form):
    side = SelectField(u'MenuItem', validators=[DataRequired()])
    quantity = IntegerField(default=1)
    submit = SubmitField("Add to Order")

    def __init__(self, *args, **kwargs):
        Form.__init__(self, *args, **kwargs)

    def validate(self):
        if not Form.validate(self):
            return False
</code></pre>

<p>The relevant views:</p>

<pre><code>@login_required
@davesbread.route('/detail/&lt;menu_item_name&gt;', methods=['GET', 'POST'])
def detail(menu_item_name):
    form = OrderForm()
    form.side.choices=[(s.id, s.name) for s in 
                    MenuItems.query.filter_by(category='side').all()]
    menu_item =   MenuItems.query.filter_by(name=menu_item_name).first_or_404()
    if form.validate_on_submit():
        add_to_order(menu_item.id)
    return render_template('customer/detail.html', title="Dave's Bread - " + menu_item.name,
                            menu_item=menu_item, form=form, user=current_user)

@login_required
@davesbread.route('/add_to_order/&lt;menu_item_id&gt;', methods=['GET', 'POST'])
def add_to_order(menu_item_id):
    id = menu_item_id
    form = OrderForm()
    side = form.side.data
    quantity = form.quantity.data
    if current_user.is_anonymous:
        return redirect(url_for('login'))
    menu_item = MenuItems.query.filter_by(id=id).first()
    order_exists = order_loader()
    if order_exists:
        order = Orders.query.filter_by(customer_id=current_user.id).first()
        order_item = OrderItems(menu_item=menu_item, quantity=quantity,
                                side_item=side)
        order.order_items.append(order_item)
        db.session.add(order)
        db.session.commit()
    else:
        order_item = OrderItems(menu_item=menu_item, quantity=quantity,
                                side_item=side)
        order = Orders(customer_id=current_user.id, 
                       created_on=datetime.datetime.now(),
                       submitted=False)
        order.order_items.append(order_item)
        db.session.add(order)
        db.session.commit()
        session['cart'] = True
        flash('Item added to order')
    return redirect(url_for('menu', category=menu_item.category))
</code></pre>

<p>The template:</p>

<pre><code>{% extends "base.html" %}

{% block content %}

&lt;table&gt;
  &lt;tr&gt;
    &lt;td&gt;
      &lt;img src="{{ menu_item.image_path }}"&gt;
    &lt;/td&gt;
    &lt;td&gt;
      &lt;h3&gt;{{ menu_item.name }}&lt;/h3&gt;
      &lt;br&gt;
      &lt;p&gt; {{ menu_item.description }}&lt;/p&gt;
    &lt;/td&gt;
    &lt;td&gt;
       &lt;form action="{{ url_for('add_to_order', menu_item_id=menu_item.id) }}" 
             method=post&gt;
        {{ form.hidden_tag() }}

        {{ form.side }}

        {{ form.quantity.label }}
        {{ form.quantity }}

        {{ form.submit }}
       &lt;/form&gt;
    &lt;/td&gt;
  &lt;/tr&gt;
&lt;table&gt;

{% endblock %}
</code></pre>

<p>Here's the traceback:
<a href="https://gist.github.com/baccb79b31728e63f252" rel="nofollow">https://gist.github.com/baccb79b31728e63f252</a></p>

<p>What's causing this error? Thanks in advance :)</p>