Hi there -

Can't reproduce. Below is a complete test case, please modify it to illustrate how you are getting this result.

from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()


class ExampleCompositeProperty(object):
    def __composite_values__(self):
        return self.a, self.b

    def __init__(self, property_1, property_2):
        if property_1 is None:
            raise ValueError
        self.a = property_1
        self.b = property_2


class A(Base):
    __tablename__ = 'a'
    id = Column(Integer, primary_key=True)
    x = composite(
        ExampleCompositeProperty,
        Column('a', String),
        Column('b', String)
    )

e = create_engine("sqlite://", echo=True)
Base.metadata.create_all(e)

a1 = A(x=ExampleCompositeProperty('a', 'b'))
s = Session(e)
s.add(a1)
s.commit()


a1.x = ExampleCompositeProperty('a', 'c')

s.commit()

print a1.x




On 02/07/2017 05:01 PM, Samer Atiani wrote:
Hello All,

Assume you have a CompositeProperty that depends on two properties,
'property_1' and 'property_2' and assume the constructor method for this
property class does something like this:

class ExampleCompositeProperty(object):
def __init__(self, property_1, property_2):
 if property_1 is None:
raise ValueError

Now say that you have a instance that looks like this:

row.example_composite_property == ExampleCompositeProperty('old_val_1',
'old_val_2')

Then you do this:

row.example_composite_property = ExampleCompositeProperty('old_val_1',
'new_val_2')

Then the code in the function below

https://github.com/zzzeek/sqlalchemy/blob/master/lib/sqlalchemy/orm/descriptor_props.py#L332

will produce the following outcome for the 'added' and 'deleted' arrays:

added = ['old_val_1', 'new_val_2']
deleted = [None, 'old_val_2']

Then when it tries to instantiate the composite class on this line:

https://github.com/zzzeek/sqlalchemy/blob/master/lib/sqlalchemy/orm/descriptor_props.py#L359

It will cause the ValueError exception raised by the __init__ method
above. Is this a problem with my model or with SqlAlchemy's get_history
implementation?

For now, I'm working around this issue by overriding get_history() and
replacing this part:

            if hist.deleted:
                deleted.extend(hist.deleted)
            else:
                deleted.append(None)

With this:

            if hist.deleted:
                deleted.extend(hist.deleted)
            elif hist.unchanged:
            deleted.extend(hist.unchanged)
            else:
                deleted.append(None)

Which seems symmetrical to what happens with the 'added' array. However,
I'm not 100% sure that this is a general solution to this problem.

Samer

--
SQLAlchemy -
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and
Verifiable Example. See http://stackoverflow.com/help/mcve for a full
description.
---
You received this message because you are subscribed to the Google
Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send
an email to sqlalchemy+unsubscr...@googlegroups.com
<mailto:sqlalchemy+unsubscr...@googlegroups.com>.
To post to this group, send email to sqlalchemy@googlegroups.com
<mailto:sqlalchemy@googlegroups.com>.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

--
SQLAlchemy - The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to