title: flask wtforms sqlalchemy AttributeError: 'str' object has no attribute '_sa_instance_state'
body: <p>I am trying to send form data in a sqlite database. There are 6 tables with one-to-many and many-to-many relationships between them.</p>
  
  <p>Here's my model:</p>
  
  <pre><code>class Event(db.Model):
      id = db.Column(db.String, default=lambda: uuid.uuid4().hex, primary_key=True)
      name = db.Column(db.String(60), nullable=False)
      date_created = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
      activities = db.relationship('Activity', backref='event', lazy='dynamic')
      participants = db.relationship('User', backref='event', lazy='dynamic')
      globalweights = db.relationship('Globalw8', backref='event', lazy='dynamic')
  
  
  relationship_table = db.Table('relationship_table', db.Column('user_id', db.Integer, db.ForeignKey('user.id'), nullable=False), db.Column('activity_id', db.Integer,db.ForeignKey('activity.id'),nullable=False), db.PrimaryKeyConstraint('user_id', 'activity_id'))
  
  
  class Activity(db.Model):
      id = db.Column(db.Integer, primary_key=True)
      name = db.Column(db.String(60), nullable=False)
      event_id = db.Column(db.Integer, db.ForeignKey('event.id'), nullable=False)
      participants = db.relationship('User', secondary=relationship_table, backref='activity')
      amounts = db.relationship('Amount', backref='activity', lazy='dynamic')
      activityweights = db.relationship('Activityw8', backref='activity', lazy='dynamic')
  
  
  class User(db.Model):
      id = db.Column(db.Integer, primary_key=True)
      name = db.Column(db.String(60), nullable=False)
      event_id = db.Column(db.Integer, db.ForeignKey('event.id'), nullable=False)
      globalw8_id = db.Column(db.Integer, db.ForeignKey('globalw8.id'), nullable=False)
      activityweights = db.relationship('Activityw8', backref='participant', lazy='dynamic')
      amounts = db.relationship('Amount', backref='participant', lazy='dynamic')
  
  
  class Amount(db.Model):
      id = db.Column(db.Integer, primary_key=True)
      amount = db.Column(db.Float, nullable=False)
      user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
      activity_id = db.Column(db.Integer, db.ForeignKey('activity.id'), nullable=False)
  
  
  class Globalw8(db.Model):
      id = db.Column(db.Integer, primary_key=True)
      weight = db.Column(db.Float, nullable=False)
      user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
      event_id = db.Column(db.Integer, db.ForeignKey('event.id'), nullable=False)
  
  
  class Activityw8(db.Model):
      id = db.Column(db.Integer, primary_key=True)
      weight = db.Column(db.Float, nullable=False)
      user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
      activity_id = db.Column(db.Integer, db.ForeignKey('activity.id'), nullable=False)
  </code></pre>
  
  <p>And here's my route:</p>
  
  <pre><code>@app.route('/', methods=['GET', 'POST'])
  def home():
      form = eveForm()
      if form.validate_on_submit():
          activity = Activity(name=form.actName.data, event=form.eveName.data)
          event = Event(name=form.eveName.data)
          user = User(name=form.partName.data, event=form.eveName.data, globalw8=form.partGlobWeight.data, activity=form.actName.data)
          globalw8 = Globalw8(weight=form.partGlobWeight.data, participant=form.partName.data, event=form.eveName.data)
          amount = Amount(amount=form.amount.data, participant=form.partName.data, activity=form.actName.data)
          activityw8 = Activityw8(weight=form.partActWeight.data, participant=form.partName.data, activity=form.actName.data)
          db.session.add(activityw8)
          db.session.add(activity)
          db.session.add(event)
          db.session.add(user)
          db.session.add(globalw8)
          db.session.add(amount)
          db.session.commit()
  </code></pre>
  
  <p>When I submit my form I get the error "AttributeError: 'str' object has no attribute '_sa_instance_state'"</p>
  
  <p>I suspect this is due to my route and how I am trying to collect the form data, in particular data related to relationships 
  <em>(ex. user = User(name=form.partName.data, event=form.eveName.data, globalw8=form.partGlobWeight.data, activity=form.actName.data)</em></p>
  
  <p>For instance in the example above, <em>event</em> is a backref and nowhere have I found what to affect to it but what I have done, i.e. <em>form.eveName.data</em></p>
  
  <p>Thanks in advance</p>
  
  <p>Edit: Here's the Traceback</p>
  
  <pre><code>Traceback (most recent call last):
    File "C:\Users\xaray\Anaconda3\envs\Flask\lib\site-packages\flask\app.py", line 2309, in __call__
      return self.wsgi_app(environ, start_response)
    File "C:\Users\xaray\Anaconda3\envs\Flask\lib\site-packages\flask\app.py", line 2295, in wsgi_app
      response = self.handle_exception(e)
    File "C:\Users\xaray\Anaconda3\envs\Flask\lib\site-packages\flask\app.py", line 1741, in handle_exception
      reraise(exc_type, exc_value, tb)
    File "C:\Users\xaray\Anaconda3\envs\Flask\lib\site-packages\flask\_compat.py", line 35, in reraise
      raise value
    File "C:\Users\xaray\Anaconda3\envs\Flask\lib\site-packages\flask\app.py", line 2292, in wsgi_app
      response = self.full_dispatch_request()
    File "C:\Users\xaray\Anaconda3\envs\Flask\lib\site-packages\flask\app.py", line 1815, in full_dispatch_request
      rv = self.handle_user_exception(e)
    File "C:\Users\xaray\Anaconda3\envs\Flask\lib\site-packages\flask\app.py", line 1718, in handle_user_exception
      reraise(exc_type, exc_value, tb)
    File "C:\Users\xaray\Anaconda3\envs\Flask\lib\site-packages\flask\_compat.py", line 35, in reraise
      raise value
    File "C:\Users\xaray\Anaconda3\envs\Flask\lib\site-packages\flask\app.py", line 1813, in full_dispatch_request
      rv = self.dispatch_request()
    File "C:\Users\xaray\Anaconda3\envs\Flask\lib\site-packages\flask\app.py", line 1799, in dispatch_request
      return self.view_functions[rule.endpoint](**req.view_args)
    File "C:\Users\xaray\code\Flaskapps\cuentapp\app.py", line 70, in home
      activity = Activity(name=form.actName.data, event=form.eveName.data)
    File "&lt;string&gt;", line 4, in __init__
  
    File "C:\Users\xaray\Anaconda3\envs\Flask\lib\site-packages\sqlalchemy\orm\state.py", line 441, in _initialize_instance
      manager.dispatch.init_failure(self, args, kwargs)
    File "C:\Users\xaray\Anaconda3\envs\Flask\lib\site-packages\sqlalchemy\util\langhelpers.py", line 68, in __exit__
      compat.reraise(exc_type, exc_value, exc_tb)
    File "C:\Users\xaray\Anaconda3\envs\Flask\lib\site-packages\sqlalchemy\util\compat.py", line 129, in reraise
      raise value
    File "C:\Users\xaray\Anaconda3\envs\Flask\lib\site-packages\sqlalchemy\orm\state.py", line 438, in _initialize_instance
      return manager.original_init(*mixed[1:], **kwargs)
    File "C:\Users\xaray\Anaconda3\envs\Flask\lib\site-packages\sqlalchemy\ext\declarative\base.py", line 836, in _declarative_constructor
      setattr(self, k, kwargs[k])
    File "C:\Users\xaray\Anaconda3\envs\Flask\lib\site-packages\sqlalchemy\orm\attributes.py", line 262, in __set__
      instance_state(instance), instance_dict(instance), value, None
    File "C:\Users\xaray\Anaconda3\envs\Flask\lib\site-packages\sqlalchemy\orm\attributes.py", line 975, in set
      value = self.fire_replace_event(state, dict_, value, old, initiator)
    File "C:\Users\xaray\Anaconda3\envs\Flask\lib\site-packages\sqlalchemy\orm\attributes.py", line 998, in fire_replace_event
      state, value, previous, initiator or self._replace_token
    File "C:\Users\xaray\Anaconda3\envs\Flask\lib\site-packages\sqlalchemy\orm\attributes.py", line 1407, in emit_backref_from_scalar_set_event
      instance_state(child),
  AttributeError: 'str' object has no attribute '_sa_instance_state'
  </code></pre>