[PonyORM-list] Entity Creation

Alexander Kozlovsky alexander.kozlovsky at gmail.com
Thu Dec 4 11:25:52 UTC 2014


Hi Bruce!

> We really require the ability to add an entity to the schema while the
using application is active.

Currently Pony doesn't allow to change or add entities to Database object
after the mapping was generated. Theoretically, in the future we can allow
it, but it will complicate internal architecture. What should we do when in
multi-threaded application one thread starts a transaction and the other
thread starts changing database entities? Currently the logic is simple,
you just create all entities and then start working with them in any thread.

> Do you have any suggestions / work-arounds (besides re-initializing the
Database object)?

If it is possible, could you describe your use-case in more detail? Why
re-initializing of the Database object is not suitable for you?

Note that you can have several Database objects working with the same
physical database. For example, you can have one Database object for
"static" entities which remains the same during your application lifetime,
and the other Database object for "dynamic" entities which is created
programmatically. When you list of "dynamic" entities is changed, you can
do db.disconnect() for previous "dynamic" db object, and then create new db
object with updated entity definitions. This is the example of how you can
create entity programmatically:

    from pony import orm
    from pony.orm.core import EntityMeta

    db = orm.Database()

    attrs = {}
    attrs['id'] = orm.PrimaryKey(int, auto=True)
    attrs['name'] = orm.Required(str)
    attrs['age'] = orm.Required(int)

    Person = EntityMeta('Person', (db.Entity,), attrs)

    db.bind('sqlite', ':memory:')
    db.generate_mapping(create_tables=True)

    with db_session:
        p1 = Person(name='John', age=20)

Tell me what do you think about having two Database objects in you
application


Regards,
Alexander Kozlovsky


On Wed, Dec 3, 2014 at 6:46 AM, Bruce Petersen <bruce.petersen at ironsafe.com>
wrote:

> Greetings!
>
> I'm seeing great advantages to Pony -- however, perhaps you can help me
> through one major stumbling block:
>
> Our app requires that tables (and their classes) be created on-the-fly.
> Given my understanding of the lifecycle of entity mappings, it looks like
> entity mappings really cannot be created dynamically.  That is, it looks
> like Pony requires that Entities need to be mapped before any using
> application starts, yes?  Any attempt to sub-class Entity results in an
> exception once the first generate_mapping is called.
>
> We really require the ability to add an entity to the schema while the
> using application is active.  We've been using SqlAlchemy to create models
> throughout the life of the application -- we were very much looking forward
> to using Pony's awesome features.
>
> Do you have any suggestions / work-arounds (besides re-initializing the
> Database object)?
>
> Many  Thanks,
>
> Bruce
>
>
>
>
>
> _______________________________________________
> ponyorm-list mailing list
> ponyorm-list at ponyorm.com
> /ponyorm-list
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: </ponyorm-list/attachments/20141204/d4d008b9/attachment.html>


More information about the ponyorm-list mailing list