title: Deleting from many-to-many SQL-Alchemy and Postgresql
body: <p>I'm trying to delete a child object from a many-to-many relationship in sql-alchemy.</p>
  
  <p>I keep getting the following error:</p>
  
  <pre><code>StaleDataError: DELETE statement on table 'headings_locations' expected to delete 1 row(s); Only 2 were matched.
  </code></pre>
  
  <p>I have looked at a number of the existing stackexchange questions
      (<a href="https://stackoverflow.com/questions/17198595/sqlalchemy-delete-error-caused-by-having-a-both-lazy-load-and-a-dynamic-version">SQLAlchemy DELETE Error caused by having a both lazy-load AND a dynamic version of the same relationship</a>, <a href="https://stackoverflow.com/questions/8024602/sqlalchemy-staledataerror-on-deleting-items-inserted-via-orm-sqlalchemy-orm-exc">SQLAlchemy StaleDataError on deleting items inserted via ORM sqlalchemy.orm.exc.StaleDataError</a>, <a href="https://stackoverflow.com/questions/28824401/sqlalchemy-attempting-to-twice-delete-many-to-many-secondary-relationship">SQLAlchemy Attempting to Twice Delete Many to Many Secondary Relationship</a>, <a href="https://stackoverflow.com/questions/16497244/delete-from-many-to-many-relationship-in-mysql">Delete from Many to Many Relationship in MySQL</a>)
  regarding this as well as read the documentation and can't figure out why it isn't working.</p>
  
  <p>My code defining the relationships is as follows:</p>
  
  <pre><code>headings_locations = db.Table('headings_locations',
          db.Column('id', db.Integer, primary_key=True),
          db.Column('location_id', db.Integer(), db.ForeignKey('location.id')),
          db.Column('headings_id', db.Integer(), db.ForeignKey('headings.id')))
  
  
  class Headings(db.Model):
      __tablename__ = "headings"
      id = db.Column(db.Integer, primary_key=True)
      name = db.Column(db.String(80))
      version = db.Column(db.Integer, default=1)
      special = db.Column(db.Boolean(), default=False)
      content = db.relationship('Content', backref=db.backref('heading'), cascade="all, delete-orphan") 
      created_date = db.Column(db.Date, default=datetime.datetime.utcnow())
      modified_date = db.Column(db.Date, default=datetime.datetime.utcnow(), onupdate=datetime.datetime.utcnow())
  
      def __init__(self, name):
          self.name = name
  
  
  
  class Location(db.Model):
  
      __tablename__ = "location"
      id = db.Column(db.Integer, primary_key=True)
      name = db.Column(db.String(80), unique=True)
      account_id = db.Column(db.Integer, db.ForeignKey('account.id')) 
      version = db.Column(db.Integer, default=1)
      created_date = db.Column(db.Date, default=datetime.datetime.utcnow())
      modified_date = db.Column(db.Date, default=datetime.datetime.utcnow())
      location_prefix = db.Column(db.Integer)
      numbers = db.relationship('Numbers', backref=db.backref('location'), cascade="all, delete-orphan") 
      headings = db.relationship('Headings', secondary=headings_locations, 
                              backref=db.backref('locations', lazy='dynamic', cascade="all"))
  
  
      def __init__(self, name):
          self.name = name
  </code></pre>
  
  <p>And my delete code is as follows:</p>
  
  <pre><code>@content_blueprint.route('/delete_content/&lt;int:location_id&gt;/&lt;int:heading_id&gt;')
  @login_required
  def delete_content(location_id, heading_id):
      import pdb
      pdb.set_trace()
      location = db.session.query(Location).filter_by(id = location_id).first()
      heading = db.session.query(Headings).filter_by(id = heading_id).first()
      location.headings.remove(heading)
      #db.session.delete(heading)
      db.session.commit()
      flash('Data Updated, thank-you')
      return redirect(url_for('content.add_heading', location_id=location_id))
  </code></pre>
  
  <p>Whichever way i try and remove the child object (db.session.delete(heading) or location.headings.remove(heading) I still get the same error.</p>
  
  <p>Any help is much appreciated.</p>
  
  <p>My database is postgresql.</p>
  
  <p>Edit:
  My code which adds the relationship:</p>
  
  <pre><code>        new_heading = Headings(form.new_heading.data)
          db.session.add(new_heading)
          location.headings.append(new_heading)
          db.session.commit()
  </code></pre>