Reputation: 2475
When I try to remove relationship beetween User and its association table friends_notifications, the applicaiton raises an error StaleDataError: DELETE statement on table 'friends_notifications' expected to delete 1 row(s); Only 0 were matched.
here is my database structure:
friends_notifications = db.Table('friends_notifications',
db.Column('user_id', db.Integer, db.ForeignKey('user.id')),
db.Column('friend_id', db.Integer, db.ForeignKey('user.id'))
)
class User(db.Model):
id = db.Column(db.Integer, primary_key = True)
username = db.Column(db.String(50), unique = True)
password = db.Column(db.String(50))
email = db.Column(db.String(100), index = True, unique =True)
age = db.Column(db.SmallInteger())
about_user = db.Column(db.String(500))
img_url = db.Column(db.String(120))
notify_friend = db.relationship('User',
secondary = friends_notifications,
primaryjoin = (friends_notifications.c.user_id == id),
secondaryjoin = (friends_notifications.c.friend_id == id),
lazy = 'dynamic'
)
def __init__(self, username, password, email, age, about_user, img_url):
self.username = username
self.password = password
self.email = email
self.age = age
self.about_user = about_user
self.img_url = img_url
and a function that deletes the notification if user presses deny
def deny_request(self, user, globalUser):
if self.notify_friend.filter(friends_notifications.c.user_id == user.id).filter(friends_notifications.c.friend_id == globalUser.id).count() == 1:
self.notify_friend.remove(user)
return self
The view code is like this:
@app.route('/deny/<username>')
@login_required def deny(username): user = User.query.filter_by(username = username).first()
if user == None:
flash ('Monkey ' + username + ' does not exists')
return redirect(url_for('notifications'))
if user == g.user:
flash ('You cannot deny your own friend request, since it doesn\'t exists')
return redirect(url_for('notifications', username = username))
globalUser = g.user
u = user.deny_request(user, globalUser)
if u is None:
flash ('Request cannot be denied from ' + username + '!')
return redirect(url_for('notifications', username = username))
db.session.add(u)
db.session.commit()
flash('Friend request to ' + username + ' was denied')
return redirect(url_for('notifications', username = username))
so when I press on the deny button that has a refence like:
<a href="{{ url_for('deny', username = notif.username ) }}">deny</a>
the StaleDataError: DELETE statement on table 'friends_notifications' expected to delete 1 row(s); Only 0 were matched.
appers
I guess it points that the recod does not exists, but in fact it does. I might doing wrong way to delete the record, so is there any other way to do it right???? :(
Upvotes: 0
Views: 1711
Reputation: 2475
The problem was solved by adding two small functions and altering the deny function, like:
#used to deny friends request from other users
def deny_request(self, user, globalUser):
if self.notify_friend.filter(friends_notifications.c.user_id == user.id).filter(friends_notifications.c.friend_id == globalUser.id).count() == 1: #checking if the request exists
return self.cancel_request(globalUser) #sending the request
#current function is used to cancel requests that are sent by logged in user
def cancel_request(self, user):
if self.request_exists(user):
self.notify_friend.remove(user)
return self
def request_exists(self, user):
return self.notify_friend.filter(friends_notifications.c.friend_id == user.id).count() > 0
Upvotes: 1