Skip to content Skip to sidebar Skip to footer

Unhashable Type Error When Modifying Sqlalchemy Models

I am using the most basic flask app practically copied from documentations, and am receiving an extremely annoying error. I've been able to trace it down but don't know how to sol

Solution 1:

I have a "solution" for this, but I want to make sure its a viable solution and not an unstable hack.

I realize that adding the ne and eq functions to the class requires the hash function to be added in order for the model to be hashable. So this is now working:

classRole(db.Model):
    id = db.Column(db.Integer(), primary_key=True)
    name = db.Column(db.String(80), unique=True)
    description = db.Column(db.String(255))

    def__init__(self, name, desc):
        self.name = name 
        self.description = desc

    def__repr__(self):
        return'<Role: {}>'.format(str(self.name))


    def__eq__(self, other):
        return (self.name == other or
                self.name == getattr(other, 'name', None))

    def__ne__(self, other):
        returnnot self.__eq__(other)

    def__hash__(self):
        returnhash(self.name)

Let me know if I've done this properly, thanks!

Post a Comment for "Unhashable Type Error When Modifying Sqlalchemy Models"