4

I am getting an error while trying to update code for: https://github.com/thrisp/flask-security from python 2.7 to 3.3

given the following most basic instance

test.py:

from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy
from flask.ext.security import Security, UserMixin, RoleMixin, \
     SQLAlchemyUserDatastore

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite://'

db = SQLAlchemy(app)
db.drop_all()

roles_users = db.Table('roles_users',
        db.Column('user_id', db.Integer(), db.ForeignKey('user.id')),
        db.Column('role_id', db.Integer(), db.ForeignKey('role.id')))

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

class User(db.Model, UserMixin):
    id = db.Column(db.Integer, primary_key=True)
    email = db.Column(db.String(255), unique=True)
    password = db.Column(db.String(255))
    last_login_at = db.Column(db.DateTime())
    current_login_at = db.Column(db.DateTime())
    last_login_ip = db.Column(db.String(100))
    current_login_ip = db.Column(db.String(100))
    login_count = db.Column(db.Integer)
    active = db.Column(db.Boolean())
    confirmed_at = db.Column(db.DateTime())
    roles = db.relationship('Role', secondary=roles_users,
                            backref=db.backref('users', lazy='dynamic'))


db.create_all()
app.security = Security(app, datastore=SQLAlchemyUserDatastore(db, User, Role))
ds = app.extensions['security'].datastore

def create_roles():
    for role in ('admin', 'editor', 'author'):
        ds.create_role(name=role)
    ds.commit()

create_roles()

def create_users(count=None):
    users = [('matt@lp.com', 'password', ['admin'], True),
             ('joe@lp.com', 'password', ['editor'], True),
             ('dave@lp.com', 'password', ['admin', 'editor'], True),
             ('jill@lp.com', 'password', ['author'], True),
             ('tiya@lp.com', 'password', [], False)]
    count = count or len(users)

    for u in users[:count]:
        pw = u[1]
        ds.create_user(email=u[0], password=pw,
                       roles=u[2], active=u[3])
    ds.commit()

create_users(1)

results in the error:

Traceback (most recent call last):
File "test.py", line 62, in <module>
create_users(1)
File "test.py", line 59, in create_users
roles=u[2], active=u[3])
File "/media/lxch/686e26f8-c6d4-4448-8fe4-c19802726dcb/projects/current/public/flask-security/flask_security/datastore.py", line 164, in create_user
user = self.user_model(**self._prepare_create_user_args(**kwargs))
File "<string>", line 4, in __init__
File "/home/lxch/.virtualenvs/p3/lib/python3.3/site-packages/sqlalchemy/orm/state.py", line 200, in _initialize_instance
return manager.original_init(*mixed[1:], **kwargs)
File "/home/lxch/.virtualenvs/p3/lib/python3.3/site-packages/sqlalchemy/ext/declarative/base.py", line 425, in _declarative_constructor
setattr(self, k, kwargs[k])
File "/home/lxch/.virtualenvs/p3/lib/python3.3/site-packages/sqlalchemy/orm/attributes.py", line 303, in __set__
instance_dict(instance), value, None)
File "/home/lxch/.virtualenvs/p3/lib/python3.3/site-packages/sqlalchemy/orm/attributes.py", line 1001, in set
lambda adapter, i: adapter.adapt_like_to_iterable(i))
File "/home/lxch/.virtualenvs/p3/lib/python3.3/site-packages/sqlalchemy/orm/attributes.py", line 1036, in _set_iterable
collections.bulk_replace(new_values, old_collection, new_collection)
File "/home/lxch/.virtualenvs/p3/lib/python3.3/site-packages/sqlalchemy/orm/collections.py", line 803, in bulk_replace
constants = existing_idset.intersection(values or ())
File "/home/lxch/.virtualenvs/p3/lib/python3.3/site-packages/sqlalchemy/util/_collections.py", line 558, in intersection
result._members.update(self._working_set(members).intersection(other))
TypeError: unhashable type: 'Role'

I'm working on trying to understand why and what exactly is:

TypeError: unhashable type: 'Role'

Any insight appreciated.

blueblank
  • 4,724
  • 9
  • 48
  • 73
  • it likely means that either the way `SQLAlchemyUserDatastore(db, User, Role)` or the way `create_user()` is being used is wrong, as I'd assume this package wants to add `Role` objects to a collection (and a `Role` object would be hashable). Seems like it's trying to add the `Role` class itself to a collection. – zzzeek Nov 02 '13 at 21:38
  • I've just looked at this again --- I will check on that, something I had not considered from my current knowledge base. – blueblank Nov 12 '13 at 17:28
  • I was doing something a little odd in that function. I've started a py3 branch for the project and updated the function to fix the issue: https://github.com/mattupstate/flask-security/blob/py3/tests/test_app/__init__.py#L138-L146 – Matt W Dec 19 '13 at 21:17

1 Answers1

8

A similar issue is discussed here: Unhashable Type error when modifying SqlAlchemy Models

A solution (or at least workaround) is to define a hash function in your Role class, something like this:

def __hash__(self):
    return hash(self.name)
Community
  • 1
  • 1
Steve Saporta
  • 4,581
  • 3
  • 30
  • 32