--- /dev/null
+.. change::
+ :tags: bug, orm, declarative
+ :tickets: 4133
+
+ Fixed bug where declarative would not update the state of the
+ :class:`.Mapper` as far as what attributes were present, when additional
+ attributes were added or removed after the mapper attribute collections had
+ already been called and memoized. Addtionally, a ``NotImplementedError``
+ is now raised if a fully mapped attribute (e.g. column, relationship, etc.)
+ is deleted from a class that is currently mapped, since the mapper will not
+ function correctly if the attribute has been removed.
from .base import _as_declarative, \
_declarative_constructor,\
- _DeferredMapperConfig, _add_attribute
+ _DeferredMapperConfig, _add_attribute, _del_attribute
from .clsregistry import _class_resolver
def __setattr__(cls, key, value):
_add_attribute(cls, key, value)
+ def __delattr__(cls, key):
+ _del_attribute(cls, key)
def synonym_for(name, map_column=False):
"""Decorator that produces an :func:`.orm.synonym` attribute in conjunction
)
else:
type.__setattr__(cls, key, value)
+ cls.__mapper__._expire_memoizations()
else:
type.__setattr__(cls, key, value)
+def _del_attribute(cls, key):
+
+ if '__mapper__' in cls.__dict__ and \
+ key in cls.__dict__ and not cls.__mapper__._dispose_called:
+ value = cls.__dict__[key]
+ if isinstance(
+ value,
+ (Column, ColumnProperty, MapperProperty, QueryableAttribute)
+ ):
+ raise NotImplementedError(
+ "Can't un-map individual mapped attributes on a mapped class.")
+ else:
+ type.__delattr__(cls, key)
+ cls.__mapper__._expire_memoizations()
+ else:
+ type.__delattr__(cls, key)
+
+
def _declarative_constructor(self, **kwargs):
"""A simple constructor that allows initialization from kwargs.
"""
_new_mappers = False
+ _dispose_called = False
def __init__(self,
class_,
def dispose(self):
# Disable any attribute-based compilation.
self.configured = True
+ self._dispose_called = True
if hasattr(self, '_configure_failed'):
del self._configure_failed
from sqlalchemy.testing import eq_, assert_raises, \
assert_raises_message, expect_warnings, is_
from sqlalchemy.ext import declarative as decl
+from sqlalchemy.ext.hybrid import hybrid_property
from sqlalchemy import exc
import sqlalchemy as sa
from sqlalchemy import testing, util
class Test(Base):
__tablename__ = 'test'
id = Column(Integer, primary_key=True)
- # MARKMARK
+
eq_(
canary.mock_calls,
[
eq_(Base.__doc__, MyBase.__doc__)
+ def test_delattr_mapped_raises(self):
+ Base = decl.declarative_base()
+
+ class Foo(Base):
+ __tablename__ = 'foo'
+
+ id = Column(Integer, primary_key=True)
+ data = Column(String)
+
+ def go():
+ del Foo.data
+
+ assert_raises_message(
+ NotImplementedError,
+ "Can't un-map individual mapped attributes on a mapped class.",
+ go
+ )
+
+ def test_delattr_hybrid_fine(self):
+ Base = decl.declarative_base()
+
+ class Foo(Base):
+ __tablename__ = 'foo'
+
+ id = Column(Integer, primary_key=True)
+ data = Column(String)
+
+ @hybrid_property
+ def data_hybrid(self):
+ return self.data
+
+ assert "data_hybrid" in Foo.__mapper__.all_orm_descriptors.keys()
+
+ del Foo.data_hybrid
+
+ assert "data_hybrid" not in Foo.__mapper__.all_orm_descriptors.keys()
+
+ assert not hasattr(Foo, "data_hybrid")
+
+ def test_setattr_hybrid_updates_descriptors(self):
+ Base = decl.declarative_base()
+
+ class Foo(Base):
+ __tablename__ = 'foo'
+
+ id = Column(Integer, primary_key=True)
+ data = Column(String)
+
+ assert "data_hybrid" not in Foo.__mapper__.all_orm_descriptors.keys()
+
+ @hybrid_property
+ def data_hybrid(self):
+ return self.data
+ Foo.data_hybrid = data_hybrid
+ assert "data_hybrid" in Foo.__mapper__.all_orm_descriptors.keys()
+
+ del Foo.data_hybrid
+
+ assert "data_hybrid" not in Foo.__mapper__.all_orm_descriptors.keys()
+
+ assert not hasattr(Foo, "data_hybrid")
+
def _produce_test(inline, stringbased):