[PonyORM-list] problems with Date field defenition

Alexander Kozlovsky alexander.kozlovsky at gmail.com
Thu Jul 31 10:55:05 UTC 2014


Hi Vadim!

The datatype that is specifed in attribute definition is a normal Python
type. In Python, `int` and `str` types can be used immediately, but `date`
and `datetime` types must be imported from stadard `datetime` module.

You can add this import to the beginning of your program:

    from datetime import date, datetime

After that, all should work without the exception:

    date = Required(date)
    updated_at = Optional(datetime)

Alternatively, you can just import `datetime` module itself:

    import datetime

After that, refer to `date` and `datetime` types as `datetime.date` and
`datetime.datetime`:

    date = Required(datetime.date)
    updated_at = Optional(datetime.datetime)

Regards,
Alexander Kozlovsky



On Thu, Jul 31, 2014 at 2:33 PM, Вадим Бажов <vadim at ideco.ru> wrote:

> Hi people !
>
> Whenever i create a date or datetime field in entity python always gives
> me an exception:
>
> "NameError: name 'date' is not defined" for "date = Required(date)" record
> according your documentation documentation
>
> "pony.orm.core.ERDiagramError: Entity definition date was not found" for
> "date = Required('date')"
>
> I am trying to run i file with folowing code:
>
> #!/usr/bin/python
>
> # -*- coding: utf-8 -*-
>
> from pony.orm import *
>
> db = Database('sqlite', 'db.sqlite', create_db=True)
>
> class subscribers(db.Entity):
>
>     id = PrimaryKey(int, auto=True)
>
>     email   = Optional(str)
>
>     date = Required(date)
>
> sql_debug(True)
>
> db.generate_mapping(create_tables=True)
>
>
> Running that code from python shell gives me the following:
>
>  from pony.orm import *
>>>>
>>>
>  db = Database('sqlite', '/tmp/db.sqlite', create_db=True)
>>>>
>>>
> GET NEW CONNECTION
>
> RELEASE CONNECTION
>
>  class subscribers(db.Entity):
>>>>
>>>
> ...     id = PrimaryKey(int, auto=True)
>
> ...     email   = Optional(str)
>
> ...     date = Required('date')
>
> ...
>
>  sql_debug(True)
>>>>
>>>
>  db.generate_mapping(create_tables=True)
>>>>
>>>
> Traceback (most recent call last):
>
>   File "<stdin>", line 1, in <module>
>
>   File "<auto generated wrapper of generate_mapping() function>", line 2,
> in generate_mapping
>
>   File "/usr/local/lib/python2.7/dist-packages/pony/orm/core.py", line
> 2698, in _resolve_attr_types_
>
>     throw(ERDiagramError, 'Entity definition %s was not found' % py_type)
>
>   File "/usr/local/lib/python2.7/dist-packages/pony/utils.py", line 126,
> in throw
>
>     raise exc  # Set "pony.options.CUT_TRACEBACK = False" to see full
> traceback
>
> pony.orm.core.ERDiagramError: Entity definition date was not found
>
>
> So database can't be created after all.
>
> Pony version installed with pip:
>
> pony                      - Pony Object-Relational Mapper
>
>   INSTALLED: 0.5.1
>
>   LATEST:    0.5-beta
>
> Is it a bug or something? How i should create a datefield with Pony ?
>
> I've couldn't create a table with several tables with Pony until i
> commented out all datefields strings from the code. So Pony works if i
> don't use datetime fields.
>
>
> --
> С уважением,
> Бажов Вадим,
> Инженер отдела технической поддержки,
> Компания Айдеко
> --
> Телефоны: +7 (495) 987-32-70; +7 (495) 662-87-34 (тех. поддержка); +7
> (343) 345-15-75; Факс: +7 (343) 383-75-13
>
> Электронная почта:
> Вопросы по приобретению: sales at ideco.ru
> Технические вопросы: support at ideco.ru
> Партнерство: partners at ideco.ru
> Общие вопросы: info at ideco.ru
>
> Сайт: http://www.ideco.ru/
> Форум: http://www.ideco.ru/forum/
>
> _______________________________________________
> ponyorm-list mailing list
> ponyorm-list at ponyorm.com
> /ponyorm-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: </ponyorm-list/attachments/20140731/6dbecc5d/attachment.html>


More information about the ponyorm-list mailing list