On Mar 5, 2010, at 6:44 PM, patrick wrote: > Well it's something between the two. The instance variable > "compressed" will always be NULL when adding or updating an instance, > but I want it to infer a value from another instance variable. When > inserting... the value of 'compressed' in the sql statement needs to > be the raw SQL string "COMPRESS('%s')" % instance.text. It isn't > simply running it through a function... it's dynamically creating the > column value from the object's instance variables upon insert or > update. MySQL has some funky compression function so the insert value > for the column has to be raw sql.
so do a before_insert() mapper extension and set the attribute as needed to "func.compressed(instance.text)". Or do the same at the object level, i.e user sets myobject.foo, "foo" is a descriptor-enabled method which then sets myobject.bar = func.compressed(foo) or whatever. the technique here is http://www.sqlalchemy.org/docs/session.html#embedding-sql-insert-update-expressions-into-a-flush . > > On Mar 4, 3:30 pm, "Michael Bayer" <mike...@zzzcomputing.com> wrote: >> patrick wrote: >>> Hey, >>> I'm trying to create dynamic defaults for columns ala "http:// >>> www.sqlalchemy.org/docs/metadata.html#context-sensitive-default-funct...". >>> MySQL has COMPRESS and UNCOMPRESS functions that I'm trying to >>> leverage. I don't want to compress with python's zlib because I have >>> legacy tables that were compressed using MySQL (which has a weird non- >>> standard zip header and body), and I need to interface with them. >>> Anyway, during an insert or update, I want to grab the 'text' variable >>> from the instance object and insert it into the database like: >>> COMPRESS("the text value"). Obviously context.current_parameters is >>> not the appropriate object, but I can't figure out if it's possible to >>> access the instance being inserted/updated. >> >> are you trying to create a *default* value for an INSERT/UPDATE when NULL >> would otherwise be passed, or are you trying to run all incoming/outgoing >> data through a SQL function ? those are two completely separate topics. >> >> >> >>> def compress_text(context): >>> return "COMPRESS('%s')" % context.current_parameters['text'] >> >>> class Tree(BaseStruct, Base): >>> __tablename__ = 'tree' >>> __table_args__ = ( >>> {'autoload':True} >>> ) >> >>> compressed = >>> deferred(Column(Binary(),default=compress_text,default=compress_text,onupdate=compress_text)) >>> text = >>> column_property(select(["UNCOMPRESS(compressed)"]),deferred=True) >> >>> Is this possible with 0.5.7? >> >>> -- >>> You received this message because you are subscribed to the Google Groups >>> "sqlalchemy" group. >>> To post to this group, send email to sqlalch...@googlegroups.com. >>> To unsubscribe from this group, send email to >>> sqlalchemy+unsubscr...@googlegroups.com. >>> For more options, visit this group at >>> http://groups.google.com/group/sqlalchemy?hl=en. > > -- > You received this message because you are subscribed to the Google Groups > "sqlalchemy" group. > To post to this group, send email to sqlalch...@googlegroups.com. > To unsubscribe from this group, send email to > sqlalchemy+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/sqlalchemy?hl=en. > -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To post to this group, send email to sqlalch...@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.