diff options
Diffstat (limited to 'oslo_db/sqlalchemy/models.py')
-rw-r--r-- | oslo_db/sqlalchemy/models.py | 10 |
1 files changed, 9 insertions, 1 deletions
diff --git a/oslo_db/sqlalchemy/models.py b/oslo_db/sqlalchemy/models.py index 4293c9d..0c4e0de 100644 --- a/oslo_db/sqlalchemy/models.py +++ b/oslo_db/sqlalchemy/models.py @@ -54,7 +54,15 @@ class ModelBase(six.Iterator): return getattr(self, key) def __contains__(self, key): - return hasattr(self, key) + # Don't use hasattr() because hasattr() catches any exception, not only + # AttributeError. We want to passthrough SQLAlchemy exceptions + # (ex: sqlalchemy.orm.exc.DetachedInstanceError). + try: + getattr(self, key) + except AttributeError: + return False + else: + return True def get(self, key, default=None): return getattr(self, key, default) |