title: Associating an SQLAlchemy object with an existing record (Python/Pyramid)
body: <p>When attempting to add a <code>Widget</code> to the (SQLite) DB, and associate it with an already existing <code>User</code> as defined below:</p>
  
  <pre><code>class Widget(Base):
      __tablename__ = 'widgets'
      id = Column(Integer, primary_key=True)
      kind = Column(Text)
      user = Column(Text, ForeignKey('users.name'))
  
  class User(Base):
      __tablename__ = 'users'
      id = Column(Integer, primary_key=True)
      name = Column(Text, unique=True)
  
  @view_config(route_name='name', renderer='json')
  def add_widget(request):
      user_name = 'foo'
      user = request.dbsession.query(User).filter_by(name=user_name).one()
      new_widget = Widget(kind='bar', user=user)
  </code></pre>
  
  <p>I receive the error <code>sqlalchemy.exc.InterfaceError: (sqlite3.InterfaceError) Error binding parameter 1 - probably unsupported type. [SQL: 'INSERT INTO widgets (kind, user) VALUES (?, ?)'] [parameters: ('bar', &lt;example.models.user.User object at 0x1050bdghd&gt;)]</code></p>
  
  <p>I can add the value for <code>user.name</code> directly, but this breaks the relationship. For example, <code>widget.user.name</code> would give an error like <code>'str' object has no attribute 'repository'</code></p>