[PonyORM-list] Changing schema during development?

Alexander Kozlovsky alexander.kozlovsky at gmail.com
Tue Oct 3 03:36:12 UTC 2017


> Is it not possible to let the database drive the structure and dynamically
create the entity classes somehow based on introspection of the database?

Yes. At this moment, Pony does not have an official way to introspect a
database and reconstruct entities from the database. Typically, a database
does not contain enough information to fully restore entity classes anyway.

> So as of today, I'd have to lose my data to add/remove fields?

During the development, it is often convenient to have a function which
populate database by some test data. The benefit of such a function is that
you can play with database, change some information, and then restore the
database to a known initial state. You can see such a function in examples
like pony.orm.examples.university1.

If you have such a function, then you can easily change entities, make
corresponding change to the function with initial data, and re-create the
database.

If your database content cannot be re-generated, at this moment you need to
write ALTER TABLE command manually. Typically they are simple. For example,
if you want to change the type of attribute from `int` to `str`, the
command for MySQL will look like:

    ALTER TABLE my_table CHANGE my_column my_column VARCHAR(20)

When we release the migration tool, it will generate and run ALTER TABLE
command automatically, basing on changes in entity classes (it will not
introspect the database, because this is less reliable)


Regards,
Alexander


On Tue, Oct 3, 2017 at 1:10 AM, Tobiah <toby at tobiah.org> wrote:

> So as of today, I'd have to lose my data to add/remove fields?
>
> Is it not possible to let the database drive the structure and
> dynamically create the entity classes somehow based on introspection
> of the database?
>
> Thanks,
>
> Tobiah
>
> On 10/02/2017 02:45 PM, Alexander Kozlovsky wrote:
>
>> Hi Tobiah!
>>
>> Soon we will release a migration tool which should help with updating the
>> database schema.
>>
>> Until this, during the early stage of development you can drop all tables
>> and re-create them:
>>
>>      # update_db.py
>>
>>      from models import db
>>      from settings import db_params
>>      from initial_data import populate_database
>>
>>      db.bind(**db_params)
>>      db.generate_mapping(check_tables=False)
>>      db.drop_all_tables(with_all_data=True)
>>      db.create_tables()
>>      populate_database()
>>
>>
>> Best regards,
>> Alexander Kozlovsky
>>
>>
>> On Tue, Oct 3, 2017 at 12:05 AM, Tobiah <toby at tobiah.org <mailto:
>> toby at tobiah.org>> wrote:
>>
>>     I'm new to ORMs in general.  I used the online tool to
>>     create a set of entities and used the resulting python
>>     class set in a python program to automatically generate
>>     a mysql database.
>>
>>     Now I've decided that I want to change one of my 'int' types to
>>     a 'str'.  I did this in the corresponding class and re-ran
>>     the program, but my database field type was not updated.
>>     How does one normally handle a changing schema during
>>     development?  Do you have to puff the database and rebuild
>>     every time there is a change?
>>
>>     Also, does anyone ever somehow generate the Pony classes at
>>     runtime based on introspection of the database?
>>
>>
>>     Thanks,
>>
>>
>>     Tobiah
>>     _______________________________________________
>>     ponyorm-list mailing list
>>     ponyorm-list at ponyorm.com <mailto:ponyorm-list at ponyorm.com>
>>     /ponyorm-list
>>     </ponyorm-list>
>>
>>
>>
>>
>> _______________________________________________
>> ponyorm-list mailing list
>> ponyorm-list at ponyorm.com
>> /ponyorm-list
>>
>> _______________________________________________
> ponyorm-list mailing list
> ponyorm-list at ponyorm.com
> /ponyorm-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: </ponyorm-list/attachments/20171003/6145f1ba/attachment-0001.html>


More information about the ponyorm-list mailing list