On Oct 28, 2010, at 4:41 AM, KLEIN Stéphane wrote:

> Hi,
> 
> in my project, I use "onupdate" attribute :
> 
> foobar_table = Table("FooBar", meta.metadata,
> ...
>    Column("created", DateTime(), default=datetime.datetime.now),
>    Column("modified", DateTime(), default=datetime.datetime.now,
> onupdate=datetime.datetime.now),
> ...
> )
> 
> All work great.
> 
> However, my project have an importation feature and I need to set
> original "modified" field value.
> 
> To do that, I've try this solution :
> 
> my_foobar_obj.modifield =
> datetime.datetime.strptime(source_date_value, '%Y-%m-%d %H:%M:%S')
> session.commit()
> 
> => not success, "modified" field not contain "source_date_value" but
> current date
> 
> Other solution :
> 
> foobar_table.update().\
>                where(foobar_table.c.id==my_foobar_obj.id).\
> 
> values(modified=datetime.datetime.strptime(source_date_value, '%Y-%m-
> %d %H:%M:%S'))
> 
> => not success, "modified" field not contain "source_date_value" but
> current date
> 
> Have you a tips to manually change "modified" field value ?

any value that you send explicitly for the column during an update overrides 
the "onupdate" value.  Below is a test case which illustrates this:

from sqlalchemy import *
import datetime

e = create_engine('sqlite://', echo=True)
m = MetaData()
t = Table('t', m, 
    Column('id', Integer, primary_key=True),
    Column('modified', DateTime(), 
                    default=datetime.datetime.now, 
                    onupdate=datetime.datetime.now
            )
)

m.create_all(e)

old_date = datetime.datetime(2009, 10, 15, 15, 45, 0)

e.execute(t.insert().values(id=1))
e.execute(t.insert().values(id=2))
e.execute(t.insert().values(id=3, modified=old_date))

e.execute(t.update().where(t.c.id==2).values(modified=old_date))

assert e.execute(
                select([t.c.id]).
                    order_by(t.c.id).
                    where(t.c.modified==old_date)
            ).fetchall() == [(2, ), (3, )]



> 
> Thanks for your help,
> Stephane
> 
> -- 
> 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.

Reply via email to