Date problem with sqlalchemy

5 views
Skip to first unread message

Michele Gambella

unread,
May 13, 2008, 6:01:39 AM5/13/08
to TurboGears
Dear Sirs,

I am facing a problem with an sqlachemy query in a turbogears project,
running on a windows xp, i386 machine.

The extract of the model file is:

#---------------------------------------------------------------------------------------------------------

tabella_operazioni = Table('operazioni', metadata,
Column('operazione_id', Integer,
primary_key=True),
Column('descrizione', Unicode(255)),
Column('data', Date),
Column('importo', Unicode(255)),
Column('contatto_id', Integer,
ForeignKey('contatti.contatto_id')),
Column('divisione_id', Integer,
ForeignKey('divisioni.divisione_id'))
)

tabella_conti = Table('conti', metadata,
Column('conto_id', Integer, primary_key=True),
Column('descrizione', Unicode(255)),
Column('data', Date),
Column('importo', Unicode(255)),
Column('operazione_id', Integer,
ForeignKey('operazioni.operazione_id'))
)

class Operazione(object):
def __init__(self, **kw):
self.divisione_id = kw['divisione_id']
self.descrizione = kw['descrizione']
self.contatto_id = kw['contatto_id']
self.data = kw['data']
self.importo = kw['importo']


class Conto(object):
def __init__(self, **kw):
self.descrizione = kw['descrizione']
self.data = kw['data']
self.importo = kw['importo']
self.operazione_id = kw['operazione_id']

mapper(Operazione, tabella_operazioni, properties={
'conti':relation(Conto, backref='operazione')
})

mapper(Conto, tabella_conti)

#---------------------------------------------------------------------------------------------------------

While my project was running, I launched the "tg-admin shell" command
and then I issued the following commands:

>>> operazioni = session.query(model.Operazione).all()
>>> operazioni
[<myerp.model.Operazione object at 0x01795A70>,
<myerp.model.Operazione object at 0x01795CB0>, <myerp.model.Operazione
object at 0x017A4E70>]
>>> a = operazioni[0]
>>> a
<myerp.model.Operazione object at 0x01795A70>
>>> a.conti
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "C:\Programmi\Python25\lib\site-packages\sqlalchemy-0.4.4-
py2.5.egg\sqlalchemy\orm\attributes.py", line 44, in __get__
return self.impl.get(instance._state)
File "C:\Programmi\Python25\lib\site-packages\sqlalchemy-0.4.4-
py2.5.egg\sqlalchemy\orm\attributes.py", line 279, in get
value = callable_()
File "C:\Programmi\Python25\lib\site-packages\sqlalchemy-0.4.4-
py2.5.egg\sqlalchemy\orm\strategies.py", line 483, in __call__
result = q.all()
File "C:\Programmi\Python25\lib\site-packages\sqlalchemy-0.4.4-
py2.5.egg\sqlalchemy\orm\query.py", line 860, in all
return list(self)
File "C:\Programmi\Python25\lib\site-packages\sqlalchemy-0.4.4-
py2.5.egg\sqlalchemy\orm\query.py", line 1007, in iterate_instances
rows.append(main(context, row))
File "C:\Programmi\Python25\lib\site-packages\sqlalchemy-0.4.4-
py2.5.egg\sqlalchemy\orm\query.py", line 955, in main
extension=context.extension,
only_load_props=context.only_load_props,
refresh_instance=context.refresh_instance
File "C:\Programmi\Python25\lib\site-packages\sqlalchemy-0.4.4-
py2.5.egg\sqlalchemy\orm\mapper.py", line 1363, in _instance
self.populate_instance(context, instance, row,
only_load_props=only_load_props, instancekey=identitykey, isnew=isnew)
File "C:\Programmi\Python25\lib\site-packages\sqlalchemy-0.4.4-
py2.5.egg\sqlalchemy\orm\mapper.py", line 1444, in populate_instance
selectcontext.exec_with_path(self, key, populator, instance, row,
ispostselect=ispostselect, isnew=isnew, **flags)
File "C:\Programmi\Python25\lib\site-packages\sqlalchemy-0.4.4-
py2.5.egg\sqlalchemy\orm\query.py", line 1549, in exec_with_path
return func(*args, **kwargs)
File "C:\Programmi\Python25\lib\site-packages\sqlalchemy-0.4.4-
py2.5.egg\sqlalchemy\orm\strategies.py", line 79, in new_execute
instance.__dict__[self.key] = row[self.columns[0]]
File "C:\Programmi\Python25\lib\site-packages\sqlalchemy-0.4.4-
py2.5.egg\sqlalchemy\engine\base.py", line 1341, in __getitem__
return self.__parent._get_col(self.__row, key)
File "C:\Programmi\Python25\lib\site-packages\sqlalchemy-0.4.4-
py2.5.egg\sqlalchemy\engine\base.py", line 1603, in _get_col
return processor(row[index])
File "C:\Programmi\Python25\lib\site-packages\sqlalchemy-0.4.4-
py2.5.egg\sqlalchemy\databases\sqlite.py", line 92, in process
tup = self._cvt(value, dialect)
File "C:\Programmi\Python25\lib\site-packages\sqlalchemy-0.4.4-
py2.5.egg\sqlalchemy\databases\sqlite.py", line 68, in _cvt
return time.strptime(value, self.__format__)[0:6] + (microsecond,)
File "C:\Programmi\Python25\lib\_strptime.py", line 330, in strptime
(data_string, format))
ValueError: time data did not match format: data=12/05/2008 fmt=%Y-
%m-%d





Why there is a ValueError? in the database every date is stored in
this format yyyy-mm-dd ...


Thanks in advance for your help.

Michele Gambella

Lukasz Szybalski

unread,
May 13, 2008, 2:45:07 PM5/13/08
to turbo...@googlegroups.com

is this date or date time field.?

yyyy-mm-dd
or
yyyy-mm-dd-HH:MM:SS

see
http://lucasmanual.com/mywiki/PythonManual#head-7b8d3475aa2baaa193b02b72fccd6eb009a1ee63

Lucas

Santiago Videla

unread,
May 13, 2008, 4:45:07 PM5/13/08
to turbo...@googlegroups.com
I'm really newbie in turbogears

But doing some tests, I had the same problem using sqlite

But when I try the same code with postgresql, it works.

I know that it's not a good solution, but I hope it helps

cheers
--
Santiago Videla
www.revolucionesweb.com.ar
http://www.linkedin.com/in/svidela

Sigue la mata dando de que hablar siempre abajo y a la izquierda donde el pensamiento que se hace corazón resplandece con la palabra sencilla y humilde que l@s tod@s somos.

Michele Gambella

unread,
May 14, 2008, 6:06:03 AM5/14/08
to TurboGears
hi guys... I solved the problem...

I was using SQlite as database, and SQlite2008pro as the guy manager
to see the values stored in it, but SQlite showed me the date in
format yyyy-mm-dd instead of dd/mm/yyyy that was the format really
stored in it... so I have been confused by that...

I noticed this behavior by using another SQLite gui manager: sqliteman

thanks for your answers
Michele



On 13 Mag, 22:45, "Santiago Videla" <santiago.vid...@gmail.com> wrote:
> I'm really newbie in turbogears
>
> But doing some tests, I had the same problem using sqlite
>
> But when I try the same code with postgresql, it works.
>
> I know that it's not a good solution, but I hope it helps
>
> cheers
>
> On Tue, May 13, 2008 at 3:45 PM, Lukasz Szybalski <szybal...@gmail.com>
> >http://lucasmanual.com/mywiki/PythonManual#head-7b8d3475aa2baaa193b02...
>
> > Lucas
>
> --
> Santiago Videlawww.revolucionesweb.com.arhttp://www.linkedin.com/in/svidela

percious

unread,
May 14, 2008, 12:25:26 PM5/14/08
to TurboGears
I have battled this problem a few times.

The object which you have mapped has no idea that the string you want
to send to sqlite needs to be a different format.

The problem is that sqlite allows you to store that string, but then
complains when you go to pull it out again.

My recommendation is to parse the string into a datetime object and
store that datetime object in your mapped object before you save it.

Or, better yet use a validator which does the same thing.

Let me know if you are still stuck after obtaining this knowledge.

-chris

On May 14, 4:06 am, Michele Gambella <michele.gambe...@gmail.com>
wrote:
Reply all
Reply to author
Forward
0 new messages