Peewee ORM - Copying data from multiple database in a main one
Asked Answered
K

1

5

I want to copy the data from multiple database, processing them, and after that moving them to a main database. All the databases have the same schema.

Even if I open and close the databases, peewee always connect to the same database(the 3rd in the list) and is not respecting the connections order.

databases = [spie_db, opticsorg_db, phcom_db]


# map to a dictionary the values from the record
def mapping(record):
    comp_d = {'name': record.name,
              'address': record.address,
              'country': record.country,
              'website': record.website,
              'domain':  record.domain
              }
    return comp_d


def merge_data():
    company_list = []
    for database in databases:
        database.connect()
        # cycle trough db
        for record in Company.select():
            # append each record to the list
            company_list.append(mapping(record))
        database.close()
    return company_list


# get data from the other databases
companies = merge_data()
# the merge database
db.connect()
# add records in the merge db
for company in companies:

    Company.create(name=company['name'], address=company['address'], country=company['country'],
                   website=company['website'], domain=company['domain'])

db.close()
Kassala answered 19/1, 2017 at 12:31 Comment(0)
A
7

You'll want to manually set the database in the model's Meta options:

Company._meta.database = db
db.connect()
for company in companies:

    Company.create(name=company['name'], address=company['address'], country=company['country'],
                   website=company['website'], domain=company['domain'])
Abarca answered 16/2, 2017 at 3:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.