How can I change manually the value of field with "onupdate" option ? I would like override "onupdate" mechanism

348 views
Skip to first unread message

KLEIN Stéphane

unread,
Oct 28, 2010, 4:41:39 AM10/28/10
to sqlalchemy
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 ?

Thanks for your help,
Stephane

Alexandre Conrad

unread,
Oct 28, 2010, 11:15:50 AM10/28/10
to sqlal...@googlegroups.com

Not sure on this one, but are you passing a formatted date string? Maybe you should set a datetime object directly and let SA do the string conversion during flush.

Alex

Sent from my fantastic HTC Hero


--
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To post to this group, send email to sqlal...@googlegroups.com.
To unsubscribe from this group, send email to sqlalchemy+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.

Thadeus Burgess

unread,
Oct 28, 2010, 3:22:33 PM10/28/10
to sqlal...@googlegroups.com
I actually have this same issue as well..

I have a field that means the record has been tested. Any updates to the field make tested = False, all except for one other field. However when I updated that field and this field it still gets reset to false..


record.approved=True
record.tested=True

db.session.commit()

record.approved is True
record.tested is False

--
Thadeus

Thadeus Burgess

unread,
Oct 28, 2010, 3:24:42 PM10/28/10
to sqlal...@googlegroups.com
The only way around this is to do two separate commits

record.approved = True
db.session.commit()
record.tested = True
db.session.commit()

record.approved is True
record.tested is True

--
Thadeus

Michael Bayer

unread,
Oct 28, 2010, 3:57:20 PM10/28/10
to sqlal...@googlegroups.com

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
>

Michael Bayer

unread,
Oct 28, 2010, 3:59:27 PM10/28/10
to sqlal...@googlegroups.com
again, no reason why that should be the case from an ORM perspective, please provide a succinct and full working test case and I can evaluate what you're doing.
Reply all
Reply to author
Forward
0 new messages