On Jul 9, 2012, at 12:38 PM, Hector Blanco wrote:
> Hello everyone.
>
> I have a class that uses a "custom" column to store a list of strings.
> The column is saved in the database using a comma sepparated string.
> When it's loaded into an instance, it "becomes" a list:
>
> class Keyword(declarativeBase):
> __tablename__ = "keywords"
> _id = Column("id", Integer, primary_key=True)
> _values = column_property(Column("values",
> CharSeparatedStrings.CharSeparatedStrings()),
> extension=ValuesAttributeExtension.ValuesAttributeExtension(),
> active_history=True)
>
> I want to use an extension to check the items that have been
> deleted/modified in that list. That's why I'm wrapping the Column in a
> column_property.
you've already got a custom type there, so unless you want an immediate
exception raise or something, this would be easier just to keep it in terms of
the column type, like a TypeDecorator around String that deals with sets on the
Python side.
>
> When I put a bunch of checkpoints in the "ValueAttributeExtension",
> the value that is going to be set is always an empty list. The
> oldvalue works fine, though:
>
> class ValuesAttributeExtension(AttributeExtension):
> def append(self, state, value, initiator):
> print("%s::append > Checkpoint!. Value: %s, state.obj()::%s" %
> (self.__class__.__name__, value, state.obj()))
> return value
> def remove(self, state, value, initiator):
> print("%s::append > Checkpoint!. Value: %s, state.obj()::%s" %
> (self.__class__.__name__, value, state.obj()))
> return value
> def set(self, state, value, oldValue, initiator):
> print("%s::set > Checkpoint!. Value: %s, oldValue: %s,
> state.obj().values: %s" % (self.__class__.__name__, value, oldValue,
> state.obj().values))
> return value
>
> Let's say the initial (old) value was ["yes"] and I add a "no". I
> would expect the "value" parameter in the set method to be ["yes",
> "no"] and the oldValue to be ["yes"] but this is what I get:
what is "adding" here ?
like keyword.values.append(x) ?
assuming "values" is a descriptor that just says, "self._values = []; return
self._values", that "self._values = []" is your set event. What you do with
the list subsequent to that is outside of SQLAlchemy's purview, that list isn't
instrumented.
you'd get the event if you said : keyword._values = ["yes"]. that's a "set"
event. the append and remove events aren't relevant here since this is not an
instrumented collection (its a column, not a relationship).
if you're really looking for the list here, set as a scalar from a mapping
perspective, to emit events as things happen to it, that's the use case for the
mutable extension:
http://docs.sqlalchemy.org/en/rel_0_7/orm/extensions/mutable.html .
if you are in fact on 0.7, I'd look to upgrade from AttributeExtension to the
event package as well.
--
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/sqlalchemy?hl=en.