当我尝试删除用户及其关联表friends_notifications之间的关系时,应用程序会引发错误StaleDataError: DELETE statement on table 'friends_notifications' expected to delete 1 row(s); Only 0 were matched.
这是我的数据库结构:
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
如果用户按下拒绝,则和删除通知的功能
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
视图代码如下:
@app.route('/deny/<username>')
@login_required def deny(用户名): 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))
所以当我按下拒绝按钮时,有一个像:
一样的拒绝<a href="{{ url_for('deny', username = notif.username ) }}">deny</a>
StaleDataError: DELETE statement on table 'friends_notifications' expected to delete 1 row(s); Only 0 were matched.
次
我想它指出recod不存在,但事实上它确实存在。我可能会以错误的方式删除记录,所以还有其他方法可以正确吗???? :(
答案 0 :(得分:1)
通过添加两个小函数并更改deny函数来解决问题,例如:
#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