detect property being assigned to more than one parent, being assigned twice

Issue #3532 new
Mike Bayer repo owner created an issue

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

Comments (4)

  1. Mike Bayer reporter

    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.

  2. Log in to comment