python - flask-SQLAlchemy + postgres unique constraint error -
i trying achieve 2 points right here:
1.- sort users bestfriend name
2.- sort users number of friend
this wrote:
class monkey(db.model): __tablename__ = 'monkey' __table_args__ = ( db.uniqueconstraint('best_friend'), ) id = db.column(db.integer, primary_key=true) name = db.column(db.string(64), index=true, unique=true) age = db.column(db.integer) email = db.column(db.string(128), index=true, unique=true) best_friend = db.column(db.string, db.foreignkey('monkey.name')) password = db.column(db.string(128)) is_admin = db.column(db.boolean) number_friends = db.column(db.integer) def __init__(self, name, age, email, password, best_friend=none, is_admin=false): self.name = name self.age = age self.email = email self.password = generate_password_hash(password) self.best_friend = best_friend self.is_admin = is_admin def number_friends(self): number = friendship.query.filter_by(monkey=self.id).count() return number def get_best_friend(self): bf = monkey.query.filter_by(id=self.best_friend).first() return bf.name def is_friend_of(self, friend): friendship = friendship.query.filter_by(monkey=self.id, friend=friend).first() if (friendship == none): return false else: return true class friendship(db.model): __tablename__ = 'friendship' __table_args__ = ( db.primarykeyconstraint('monkey', 'friend'), ) monkey = db.column(db.integer, db.foreignkey('monkey.id')) friend = db.column(db.integer, db.foreignkey('monkey.id')) def __init__(self, monkey, friend): self.monkey = monkey self.friend = friend
so, can see can number of friends monkey using method number_friends()
, problem cant figure out how use number order query
what can do?
here tried
monkeys = models.monkey.query.order_by(models.monkey.number_friends()).paginate(page, 5, false)
so far know doesnt because have make instantiation use method right? there way order query way want sqlalchemy using method number_friends()
?
Comments
Post a Comment