Transfer data between models

13 views
Skip to first unread message

florian6973

unread,
Mar 31, 2021, 10:01:21 AM3/31/21
to peewee-orm
Hi!

Would you know how I can write a method which reads a database with old peewee models and which transfers data to the new models (and a new database)?

I saw that migration can create or drop columns, but it is not what I would like, that is to say to transfer data.

Thanks.

Charles Leifer

unread,
Mar 31, 2021, 11:03:47 AM3/31/21
to peewe...@googlegroups.com
Typically you would just write the Python code to do it, or construct a query using

NewModel.insert_from(OldModel.select(...)).execute()

--
You received this message because you are subscribed to the Google Groups "peewee-orm" group.
To unsubscribe from this group and stop receiving emails from it, send an email to peewee-orm+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/peewee-orm/4fc2e00d-287c-482e-a995-ec188bab8019n%40googlegroups.com.

florian6973

unread,
Mar 31, 2021, 11:33:38 AM3/31/21
to peewee-orm
Thanks for your answer! So the best way is to have two files with the old and the new models, but with a common db instance ? The problem is that the OldModel has the same name as the NewModel (in the database, the name of the tables are the same). How can I do that?

Charles Leifer

unread,
Mar 31, 2021, 11:42:16 AM3/31/21
to peewe...@googlegroups.com
Python supports namespacing via module or import X as Y.

import old
import new
new.Model.insert_from(old.Model.select())

Or

from old import Model as OldModel
from new import Model

florian6973

unread,
Mar 31, 2021, 11:52:37 AM3/31/21
to peewee-orm
Thanks a lot! I will try this :)
Reply all
Reply to author
Forward
0 new messages