title: One-to-many Flask | SQLAlchemy
body: <p>I am trying to create a one-to-many relationship using Flask and SQLAlchemy.</p>
  
  <p>I want the one-to-many relationship to be as so:</p>
  
  <p>"For any single movie, there can be multiple characters"</p>
  
  <p>Here it what I have so far, but it is saving in my DB as one-to-one right now. (One movie to one character, saving multiple times in DB for multiple characters)</p>
  
  <pre><code>class Movie(db.Model):
      __tablename__ = "movies"
      id = db.Column('movies_id', db.Integer, primary_key=True)
      movie_type = db.Column('movie_type', db.Text())
  
      def __init__(self, movie_type):
          self.movie_type = movie_type
  
      def __repr__(self):
          return '&lt;Movie %r&gt;' % self.id
  
  class Character(db.Model):
      __tablename__ = "characters"
      id = db.Column('character_id', db.Integer, primary_key=True) 
      character_description = db.Column('character_description', db.Text())
  
      movie_id = db.Column(db.Integer, db.ForeignKey('movies.movie_id'))
      movie = db.relationship('Movie', backref='characters', lazy='dynamic')
  
      def __init__(self, character_description, movie):
          self.character_description = character_description
  
          self.movie = movie
  
      def __repr__(self):
          return '&lt;Character %r&gt;' % self.id
  </code></pre>
  
  <p>I am saving into the DB like this:</p>
  
  <pre><code>movie = models.movie(movie_type)
  character = models.Character(character_description, movie)
  
  db.session.add(movie)
  db.session.add(character)
  db.session.commit()
  </code></pre>
  
  <p>The end goal is to be able to look up what movie a character is in. If you could also help me out with that query, that would be great!</p>
  
  <p>Thanks ahead of time.</p>