I'm stuck with a SqlAlchemy problem.
I just want to delete an relation. This relation is made by an association object.
models
class User(db.Model, UserMixin):id = db.Column(db.Integer, primary_key=True)email = db.Column(db.String(255), unique=True)username = db.Column(db.String(255), unique=True)password = db.Column(db.String(255))following = db.relationship('Follower', foreign_keys='Follower.user_id')followed_by = db.relationship('Follower', foreign_keys='Follower.follow_user_id')def __repr__(self):return '<%s (%i)>' % (self.username, self.id)class Follower(db.Model):__tablename__ = 'followers'user_id = db.Column(db.Integer, db.ForeignKey('user.id'), primary_key=True)follow_user_id = db.Column(db.Integer, db.ForeignKey('user.id'), primary_key=True)created_at = db.Column(db.DateTime, default=datetime.datetime.now)user_followed = db.relationship("User", primaryjoin=(follow_user_id==User.id))user = db.relationship("User", primaryjoin=(user_id==User.id))def __repr__(self):return '<%i %i>' % (self.user_id, self.follow_user_id)
How I add a relation (it works !):
u1 = # user 1
u2 = # user 2...f = Follower()
f.user_followed = u2
u1.following.append(f)
db.session.commit()
How I try do delete a relation (it doesn't work):
f = Follower()
f.user_followed = u2u1.following.remove(f)
db.session.commit()
The error
ValueError: list.remove(x): x not in list
I understand why it doesn't work, it's because this Follower() instance is not in the list u1.following. So, how can I delete this relation?