need active history for many-to-ones if use_get is false

Issue #1737 resolved
Mike Bayer repo owner created an issue

such as when the foreign key references a non primary key:

from sqlalchemy import *
from sqlalchemy.orm import *

metadata = MetaData()

table_a = Table("table_a", metadata,
                Column("sno", Integer, primary_key=True),
                Column("uuid", String, nullable=False, unique=True),
                )

table_b = Table("table_b", metadata,
                Column("sno", Integer, Sequence("table_b_sno_seq"), primary_key=True),
                Column("a_id", String, ForeignKey('table_a.uuid'), nullable=False),
                )

class A(object):
    def __init__(self, uuid):
        self.uuid = uuid

class B(object):
    pass

mapper(A, table_a)
mapper(B, table_b, properties={"a": relation(A, backref="bs")})

engine = create_engine('sqlite://', echo=True)
Session = sessionmaker(bind=engine)

metadata.create_all(engine)

session = Session()
a1, a2 = A("uuid1"), A("uuid2")
session.add_all([a2](a1,))
a1.bs = [   B(), B()
](
)
session.commit()
a1, a2 = session.query(A).all()

for b in list(a1.bs):
    b.a = a2
session.delete(a1)
session.commit()

the fix is to change the threshold of "active history" for many-to-ones to be for any non-use_get relation, since the "we don't need active history" thing relies upon lazyload using query.get():

--- a/lib/sqlalchemy/orm/strategies.py  Sat Mar 13 16:12:29 2010 -0500
+++ b/lib/sqlalchemy/orm/strategies.py  Sat Mar 13 19:58:16 2010 -0500
@@ -377,7 +377,7 @@
                 callable_=self._class_level_loader,
                 uselist = self.parent_property.uselist,
                 typecallable = self.parent_property.collection_class,
-                active_history = self.parent_property.direction is not interfaces.MANYTOONE, 
+                active_history = not self.use_get, 
                 )

     def lazy_clause(self, state, reverse_direction=False, alias_secondary=False, adapt_source=None):

Comments (3)

  1. Log in to comment