title: SQLAlchemy insert many-to-one entries
body: <p>Sorry, if this is a newbie question but the documentation about the <a href="https://sqlalchemy.readthedocs.org/en/rel_0_8/orm/relationships.html#many-to-one" rel="nofollow">many-to-one relationship</a> doesn't seems to cover this. I have been looking for something similar to <a href="http://python.dzone.com/articles/simple-sqlalchemy-07-08" rel="nofollow">this</a> (under the "How to Insert / Add Data to Your Tables" section), however in the shown example this is always a unique insertion. </p>
  
  <p>Basically, I want to populate my database with data located on my local machine. For the sake of simplicity I have constructed the below-shown example into a <strong>MWE</strong> that illustrates the problem. The problem consists of two tables called <code>Price</code> and <code>Currency</code> and the implementation is done in a declarative style.</p>
  
  <p>
  <strong>model.py</strong></p>
  
  <pre class="lang-py prettyprint-override"><code>from sqlalchemy import Column, Integer, String
  from sqlalchemy import Float, BigInteger, ForeignKey
  from sqlalchemy.orm import relationship, backref
  from sqlalchemy.ext.declarative import declarative_base
  
  Base = declarative_base()
  
  class Currency(Base):
      __tablename__ = 'Currency'
  
      id = Column(Integer, primary_key=True)
      unit = Column(String(16), unique=True)
  
      def __init__(self, unit):
          self.unit = unit
  
  class Price(Base):
      __tablename__ = 'Price'
  
      id = Column(BigInteger, primary_key=True)
  
      currency_id = Column(Integer, ForeignKey("Currency.id"), nullable=False)
      currency = relationship("Currency", backref="Currency.id")
  
      hour1 = Column(Float)
      hour2 = Column(Float)
  
      def __init__(self, hour1, hour2):
          self.hour1 = hour1
          self.hour2 = hour2
  </code></pre>
  
  <p>Currently, I am populating the database using following code:</p>
  
  <p><strong>script.py</strong></p>
  
  <pre class="lang-py prettyprint-override"><code>from sqlalchemy import create_engine
  from sqlalchemy.orm import scoped_session, sessionmaker
  
  from model import *
  
  engine = create_engine('sqlite:///example.db', echo=True)
  
  db_session = scoped_session(sessionmaker(autocommit=False,
                                           autoflush=False,
                                           bind=engine))
  session = db_session()
  
  Base.metadata.create_all(engine)
  
  oPrice = Price(2.5, 2.5)
  oPrice.currency = Currency("EUR")
  session.add(oPrice)
  
  tPrice = Price(5.5, 1.5)
  tPrice.currency = Currency("EUR")
  session.add(tPrice)
  
  session.commit()
  </code></pre>
  
  <p>This creates an error</p>
  
  <p><code>sqlalchemy.exc.IntegrityError: (IntegrityError) column unit is not unique u'INSERT INTO "Currency" (unit) VALUES (?)' ('EUR',)</code></p>
  
  <p>What is the best strategy for populating my database, such that I ensure that my Currency.id and Price.currency_id mapping is correct? Should I make the model-classes look for uniqueness before they are initialized, and do I do that in associated with the other table?</p>