sqlalchemy
sqlalchemy copied to clipboard
detect property being assigned to more than one parent, being assigned twice
Migrated issue, originally created by Michael Bayer (@zzzeek)
e.g.:
from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
class A(Base):
__tablename__ = 'a'
id = Column(Integer, primary_key=True)
bs = relationship("B")
b_1 = bs
class B(Base):
__tablename__ = 'b'
id = Column(Integer, primary_key=True)
a_id = Column(ForeignKey('a.id'))
e = create_engine("sqlite://", echo=True)
Base.metadata.create_all(e)
configure_mappers()
print A.bs.impl
print A.b_1.impl
the basic idea, but this should be at most a warning in 1.0, error in 1.1:
diff --git a/lib/sqlalchemy/orm/interfaces.py b/lib/sqlalchemy/orm/interfaces.py
index cd4a011..19f4e0f 100644
--- a/lib/sqlalchemy/orm/interfaces.py
+++ b/lib/sqlalchemy/orm/interfaces.py
@@ -147,6 +147,8 @@ class MapperProperty(_MappedAttribute, InspectionAttr, util.MemoizedSlots):
setup when the mapper is first known.
"""
+ if getattr(self, "parent", None) is not None:
+ raise Exception("oof")
self.parent = parent
def instrument_class(self, mapper):
needs: 1. tests 2. nice message 3. appropriate exception class 4. for 1.0 backport a warning 5. migration notes 6. changelog
Michael Bayer (@zzzeek) wrote:
tests should be mapper level in test/orm/test_mapper using add_property() as well as in test/ext/declarative/test_basic using attribute set.
jvanasco (@jvanasco) wrote:
Cross linking this to group posting - https://groups.google.com/forum/#!topic/sqlalchemy/UUmRWkHW4W0
And noting the error in 1.0.8 is: File "build/bdist.macosx-10.6-intel/egg/sqlalchemy/orm/instrumentation.py", line 111, in _scalar_loader_impls self.values() if attr.impl.accepts_scalar_loader]) AttributeError: 'NoneType' object has no attribute 'accepts_scalar_loader'
reconfirmed still exists in 1.4
Mike Bayer has proposed a fix for this issue in the main branch:
warn for other mapper property objects assigned twice https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/4745