使用 SQLAlchemy,我需要将用户分配到池中,并对他们所在的每个池进行排名

Using SQLAlchemy, I need to assign users into pools, and a ranking for each pool they're in

这里是相关代码。

我正在编写一个 Flask 应用程序,用户可以在其中加入池并与池中的其他人竞争(目前,池和用户处于多对多关系中)。每个用户都需要对 he/she 所在的每个池进行评级(Elo 评级),我不确定如何将其实施到我现有的结构中。任何适合我的解决方案或更改它的实施建议都将不胜感激。

(现在,每个用户都有一个评级,但它在池之间共享,这对 Elo 没有意义)

def load_user(user_id):
    return User.query.get(int(user_id))

pool_user = db.Table('pool_user', 
    db.Column('pool_id', db.Integer, db.ForeignKey('pool.id')),
    db.Column('user_id', db.Integer, db.ForeignKey('user.id')))

class User(db.Model, UserMixin):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(20), unique=True, nullable=False)
    email = db.Column(db.String(120), unique=True, nullable=False)
    image_file = db.Column(db.String(20), nullable=False, default='default.jpg')
    password = db.Column(db.String(60), nullable=False)
    rating = db.Column(db.Integer, nullable=False)
    pools = db.relationship('Pool', secondary=pool_user, backref=('members'))

    def get_reset_token(self, expires_sec=1800):
        s = Serializer(current_app.config['SECRET_KEY'])
        return s.dumps({'user_id': self.id}).decode('utf-8')
    
    @staticmethod
    def verify_reset_token(token):
        s = Serializer(current_app.config['SECRET_KEY'])
        try:
            user_id = s.loads(token)['user_id']
        except:
            return None
        return User.query.get(user_id)

    def __repr__(self):
        return f"User('{self.username}', '{self.email}', '{self.image_file}', '{self.rating}')"

class Pool(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(20), nullable=False)

    def __repr__(self):
        return f"Pool: {self.name}"

对我来说,您应该将关联 table (pool_user) 替换为关联模型 (UserPool)。

def load_user(user_id):
    return User.query.get(int(user_id))

class User(db.Model, UserMixin):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(20), unique=True, nullable=False)
    email = db.Column(db.String(120), unique=True, nullable=False)
    image_file = db.Column(db.String(20), nullable=False, default='default.jpg')
    password = db.Column(db.String(60), nullable=False)
    pools = db.relationship('UserPool')

    def get_reset_token(self, expires_sec=1800):
        s = Serializer(current_app.config['SECRET_KEY'])
        return s.dumps({'user_id': self.id}).decode('utf-8')
    
    @staticmethod
    def verify_reset_token(token):
        s = Serializer(current_app.config['SECRET_KEY'])
        try:
            user_id = s.loads(token)['user_id']
        except:
            return None
        return User.query.get(user_id)

    def __repr__(self):
        return f"User('{self.username}', '{self.email}', '{self.image_file}'"

class Pool(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(20), nullable=False)
    members = db.relationship('UserPool')

    def __repr__(self):
        return f"Pool: {self.name}"

class UserPool(db.Model):
    pool_id = db.Column(db.Integer, db.ForeignKey('pool.id'), primary_key=True),
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'), primary_key=True)
    rating = db.Column(db.Integer, nullable=False)