sqlalchemy icon indicating copy to clipboard operation
sqlalchemy copied to clipboard

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

Open sqlalchemy-bot opened this issue 9 years ago • 8 comments

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

sqlalchemy-bot avatar Sep 16 '15 22:09 sqlalchemy-bot

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.

sqlalchemy-bot avatar Sep 16 '15 22:09 sqlalchemy-bot

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'

sqlalchemy-bot avatar Sep 17 '15 15:09 sqlalchemy-bot

Michael Bayer (@zzzeek) wrote:

minor issue so pushing out

sqlalchemy-bot avatar Jul 09 '18 22:07 sqlalchemy-bot

Changes by Michael Bayer (@zzzeek):

  • changed milestone from "1.1" to "1.2"

sqlalchemy-bot avatar May 31 '16 14:05 sqlalchemy-bot

Changes by Michael Bayer (@zzzeek):

  • added labels: low priority

sqlalchemy-bot avatar May 22 '17 19:05 sqlalchemy-bot

Changes by Michael Bayer (@zzzeek):

  • changed milestone from "1.2" to "1.3"

sqlalchemy-bot avatar May 22 '17 19:05 sqlalchemy-bot

Changes by Michael Bayer (@zzzeek):

  • changed milestone from "1.3" to "1.5"

sqlalchemy-bot avatar Jul 09 '18 22:07 sqlalchemy-bot

reconfirmed still exists in 1.4

zzzeek avatar Apr 11 '21 14:04 zzzeek

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

sqla-tester avatar Jun 14 '23 14:06 sqla-tester