Fix observer crashing, refs #168

This commit is contained in:
Konsta Vesterinen
2015-10-30 10:54:01 +02:00
parent 91fc57f805
commit 25e47a6030
4 changed files with 70 additions and 4 deletions

View File

@@ -4,6 +4,18 @@ Changelog
Here you can see the full list of changes between each SQLAlchemy-Utils release.
0.31.2 (2015-10-30)
^^^^^^^^^^^^^^^^^^^
- Fixed observes crashing when observable root_obj is ``None`` (#168)
0.31.1 (2015-10-26)
^^^^^^^^^^^^^^^^^^^
- Column observers only notified when actual changes have been made to underlying columns (#138)
0.31.0 (2015-09-17)
^^^^^^^^^^^^^^^^^^^

View File

@@ -93,4 +93,4 @@ from .types import ( # noqa
WeekDaysType
)
__version__ = '0.31.1'
__version__ = '0.31.2'

View File

@@ -245,9 +245,10 @@ class PropertyObserver(object):
root_objs = [root_objs]
for root_obj in root_objs:
args = self.get_callback_args(root_obj, callback)
if args:
yield args
if root_obj:
args = self.get_callback_args(root_obj, callback)
if args:
yield args
def get_callback_args(self, root_obj, callback):
session = sa.orm.object_session(root_obj)

View File

@@ -0,0 +1,53 @@
import sqlalchemy as sa
from sqlalchemy_utils.observer import observes
from tests import TestCase
class TestObservesForOneToManyToOneToMany(TestCase):
dns = 'postgres://postgres@localhost/sqlalchemy_utils_test'
def create_models(self):
class Device(self.Base):
__tablename__ = 'device'
id = sa.Column(sa.Integer, primary_key=True)
name = sa.Column(sa.String)
class Order(self.Base):
__tablename__ = 'order'
id = sa.Column(sa.Integer, primary_key=True)
device_id = sa.Column(
'device', sa.ForeignKey('device.id'), nullable=False
)
device = sa.orm.relationship('Device', backref='orders')
class SalesInvoice(self.Base):
__tablename__ = 'sales_invoice'
id = sa.Column(sa.Integer, primary_key=True)
order_id = sa.Column(
'order',
sa.ForeignKey('order.id'),
nullable=False
)
order = sa.orm.relationship(
'Order',
backref=sa.orm.backref(
'invoice',
uselist=False
)
)
device_name = sa.Column(sa.String)
@observes('order.device')
def process_device(self, device):
self.device_name = device.name
self.Device = Device
self.Order = Order
self.SalesInvoice = SalesInvoice
def test_observable_root_obj_is_none(self):
order = self.Order(device=self.Device(name='Something'))
self.session.add(order)
self.session.flush()