diff --git a/sqlalchemy_utils/types/password.py b/sqlalchemy_utils/types/password.py index 2b050c9..5d95e80 100644 --- a/sqlalchemy_utils/types/password.py +++ b/sqlalchemy_utils/types/password.py @@ -143,6 +143,9 @@ class PasswordType(types.TypeDecorator, ScalarCoercible): return Password(value, self.context) def _coerce(self, value): + if value is None: + return + if not isinstance(value, Password): # Hash the password using the default scheme. value = self.context.encrypt(value).encode('utf8') diff --git a/tests/types/test_password.py b/tests/types/test_password.py index 7115ce6..8b2a116 100644 --- a/tests/types/test_password.py +++ b/tests/types/test_password.py @@ -100,3 +100,17 @@ class TestPasswordType(TestCase): # Not sure what to assert here; the test raised an error before. assert obj.password != other.password + + def test_set_none(self): + + obj = self.User() + obj.password = None + + assert obj.password is None + + self.session.add(obj) + self.session.commit() + + obj = self.session.query(self.User).get(obj.id) + + assert obj.password is None