我已经与ORM对象用户建立了邻接关系:
class User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
parent_id = Column(Integer, ForeignKey('users.id')) # for self-referential voted_users
username = Column(String(16), index=True, unique=True, nullable=False)
password_hash = Column(String(128), nullable=False)
avatar = Column(Boolean, default=False, nullable=False)
skill = Column(Integer, default=1000, nullable=False)
arena_id = Column(Integer, ForeignKey('arenas.id'))
arena = relationship("Arena", back_populates="players")
entry = Column(Boolean, default=False, nullable=False)
# entry_submitted = Column(Boolean, default=False, nullable=False)
votes_pouch = Column(Integer, default=VOTES_PER_PLAYER, nullable = False)
voted_users = relationship("User", post_update=True)
votes_received = Column(Integer, default = 0, nullable = False)
notifications = relationship("Notification")
def toggle_vote(self, other):
if not other.entry:
print("invalid", other)
return
if other in self.voted_users:
print("unvote")
self.unvote(other)
else:
print("vote")
self.vote(other)
def unvote(self, other):
self.votes_pouch += 1
self.voted_users.remove(other) #not persisting??
other.votes_received -= 1
self.arena.vote_count -= 1
def vote(self, other):
if self.votes_pouch <= 0 and self not in other.votes_received:
return
self.votes_pouch -= 1
self.voted_users.append(other)
other.votes_received += 1
self.arena.vote_count += 1
每当调用表决时,它将用户添加到关系中就很好,但是当调用unvote时,会将用户从关系中删除,但是在请求完成后,另一个用户又回到了集合中,但是所有其他更改(例如由于投票数持续存在,导致永久投票:(
这是在unvote()提交之后的sql操作:
2018-08-02 12:00:58,523 INFO sqlalchemy.engine.base.Engine SELECT users.id AS u
sers_id, users.parent_id AS users_parent_id, users.username AS users_username, users.password_hash AS users_password_hash, users.avatar AS users_avatar, users.skill AS users_skill, users.arena_id AS users_arena_id, users.entry AS users_entry, users.votes_pouch AS users_votes_pouch, users.votes_received AS users_votes_received
FROM users
WHERE %s = users.arena_id
2018-08-02 12:00:58,523 INFO sqlalchemy.engine.base.Engine (1,)
2018-08-02 12:00:58,525 INFO sqlalchemy.engine.base.Engine SELECT users.id AS users_id, users.parent_id AS users_parent_id, users.username AS users_username, users.password_hash AS users_password_hash, users.avatar AS users_avatar, users.skill AS users_skill, users.arena_id AS users_arena_id, users.entry AS users_entry, users.votes_pouch AS users_votes_pouch, users.votes_received AS users_votes_received
FROM users
WHERE %s = users.parent_id
2018-08-02 12:00:58,525 INFO sqlalchemy.engine.base.Engine (2,)
unvote
2018-08-02 12:00:58,528 INFO sqlalchemy.engine.base.Engine UPDATE arenas SET vote_count=%s WHERE arenas.id = %s
2018-08-02 12:00:58,528 INFO sqlalchemy.engine.base.Engine (-4, 1)
2018-08-02 12:00:58,532 INFO sqlalchemy.engine.base.Engine UPDATE users SET votes_received=%s WHERE users.id = %s
2018-08-02 12:00:58,532 INFO sqlalchemy.engine.base.Engine (-4, 1)
2018-08-02 12:00:58,534 INFO sqlalchemy.engine.base.Engine UPDATE users SET votes_pouch=%s WHERE users.id = %s
2018-08-02 12:00:58,535 INFO sqlalchemy.engine.base.Engine (7, 2)
2018-08-02 12:00:58,537 INFO sqlalchemy.engine.base.Engine COMMIT