How to copy django objects from one DB to the other

Sometimes you need  two databases connections to synchronize objects from one django database to the other.

Setting

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    },
    'old_db': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'old_db/db_24.11.2022.sqlite3',
    },
}

Command


from django.db import models, IntegrityError, DatabaseError
from django.core.management.base import BaseCommand
from page.models import *
import sys, traceback

class Command(BaseCommand):
def handle(self, *args, **options):

pages_old = StaticPage.objects.using('old_db').filter(url_source__icontains='https://www..be/', added__gt='2022-11-20') # YYYY-MM-DD
for page in pages_old:
  page.save(using='default')

 Django documentation

https://docs.djangoproject.com/en/4.1/topics/db/multi-db/ 


!!!!!  Note this way forces the ID and internet database ID to be out of sync


IntegrityError at /admin/VehicleDealerManagement/vehicle/add/

duplicate key value violates unique constraint "VehicleDealerManagement_vehicle_pkey"
DETAIL:  Key (id)=(1) already exists.

See more information about how to fix this problem.

https://www.webdeveloper.today/2022/06/duplicate-key-value-violates-unique.html 

 

What is conclusion of this crazy story? 

1. Use python convention for app naming and always use it. 

 An app's name should follow Pep 8 Guidelines, namely it should be short, all-lowercase and not include numbers, dashes, periods, spaces, or special characters. It also, in general, should be the plural of an app's main model, so our posts app would have a main model called Post. Modules should have short, all-lowercase names. Underscores can be used in the module name if it improves readability. Python packages should also have short, all-lowercase names, although the use of underscores is discouraged. So, 1 and 3 are both valid, but 3 would be the recommended approach.

2. Even in development always use correct database Postgresql. Never use sqlite, even in development. Because moving data can sometimes take 1 or two days. 

 To create and use abstraction a postgresql you can use it via pgBounce.. 

 

SELECT nextval('auth_user_id_seq');
SELECT MAX(id) FROM auth_user;

SELECT setval('auth_user_id_seq', (SELECT MAX(id) FROM auth_user)+1);


SELECT nextval('auth_permission_id_seq');
SELECT MAX(id) FROM auth_permission;

SELECT setval(‘auth_permission_id_seq', (SELECT MAX(id) FROM auth_permission)+1);



SELECT nextval(AccountingPeriod_accountingperiod_id_seq);
SELECT MAX(id) FROM auth_permission;

SELECT MAX(id) FROM AccountingPeriod_accountingperiod;


SELECT MAX(id) FROM  "AccountingPeriod_accountingperiod";
SELECT nextval('"AccountingPeriod_accountingperiod_id_seq"');
SELECT setval('"AccountingPeriod_accountingperiod_id_seq"', (SELECT MAX(id) FROM "AccountingPeriod_accountingperiod")+1);

SELECT MAX(id) FROM  "BankExport_bankexport";
SELECT nextval('"BankExport_bankexport_id_seq"');
SELECT setval('"BankExport_bankexport_id_seq"', (SELECT MAX(id) FROM "BankExport_bankexport")+1);


SELECT MAX(id) FROM  "Document_document";
SELECT nextval('"Document_document_id_seq"');
SELECT setval('"Document_document_id_seq"', (SELECT MAX(id) FROM "Document_document")+1);


SELECT MAX(id) FROM  "Location_location";
SELECT nextval('"Location_location_id_seq"');
SELECT setval('"Location_location_id_seq"', (SELECT MAX(id) FROM "Location_location")+1);

SELECT MAX(id) FROM  "Multiposter_placement";
SELECT nextval('"Multiposter_placement_id_seq"');
SELECT setval('"Multiposter_placement_id_seq"', (SELECT MAX(id) FROM "Multiposter_placement")+1);


SELECT MAX(id) FROM  "PaymentMethod_paymentmethod";
SELECT nextval('"PaymentMethod_paymentmethod_id_seq"');
SELECT setval('"PaymentMethod_paymentmethod_id_seq"', (SELECT MAX(id) FROM "PaymentMethod_paymentmethod")+1);


SELECT MAX(id) FROM  "Transaction_transaction";
SELECT nextval('"Transaction_transaction_id_seq"');
SELECT setval('"Transaction_transaction_id_seq"', (SELECT MAX(id) FROM "Transaction_transaction")+1);



SELECT MAX(id) FROM  "VehicleDealerManagement_vehicle";
SELECT nextval('"VehicleDealerManagement_vehicle_id_seq"');
SELECT setval('"VehicleDealerManagement_vehicle_id_seq"', (SELECT MAX(id) FROM "VehicleDealerManagement_vehicle")+1);



SELECT MAX(id) FROM  "VehicleDealerManagement_vehiclecost";
SELECT nextval('"VehicleDealerManagement_vehiclecost_id_seq"');
SELECT setval('"VehicleDealerManagement_vehiclecost_id_seq"', (SELECT MAX(id) FROM "VehicleDealerManagement_vehiclecost")+1);


SELECT MAX(id) FROM  "VehicleDealerManagement_vehicledocument";
SELECT nextval('"VehicleDealerManagement_vehicledocument_id_seq"');
SELECT setval('"VehicleDealerManagement_vehicledocument_id_seq"', (SELECT MAX(id) FROM "VehicleDealerManagement_vehicledocument")+1);


SELECT MAX(id) FROM  "VehicleDealerManagement_vehicleimage";
SELECT nextval('"VehicleDealerManagement_vehicleimage_id_seq"');
SELECT setval('"VehicleDealerManagement_vehicleimage_id_seq"', (SELECT MAX(id) FROM "VehicleDealerManagement_vehicleimage")+1);


SELECT MAX(id) FROM  "VehicleMerk_vehiclemerk";
SELECT nextval('"VehicleMerk_vehiclemerk_id_seq"');
SELECT setval('"VehicleMerk_vehiclemerk_id_seq"', (SELECT MAX(id) FROM "VehicleMerk_vehiclemerk")+1);


SELECT MAX(id) FROM  "auth_permission_id_seq";
SELECT nextval('"VehicleMerk_vehiclemodel_id_seq"');
SELECT setval('"VehicleMerk_vehiclemodel_id_seq"', (SELECT MAX(id) FROM "VehicleMerk_vehiclemodel")+1);



 Schema |                      Name                      |   Type   |   Owner   
--------+------------------------------------------------+----------+-----------
 public | AccountingPeriod_accountingperiod              | table    | quickbook
 public | AccountingPeriod_accountingperiod_id_seq       | sequence | quickbook
 public | BankExport_bankexport                          | table    | quickbook
 public | BankExport_bankexport_id_seq                   | sequence | quickbook
 public | Document_document                              | table    | quickbook
 public | Document_document_id_seq                       | sequence | quickbook
 public | Location_location                              | table    | quickbook
 public | Location_location_id_seq                       | sequence | quickbook
 public | Multiposter_placement                          | table    | quickbook
 public | Multiposter_placement_id_seq                   | sequence | quickbook
 public | PaymentMethod_paymentmethod                    | table    | quickbook
 public | PaymentMethod_paymentmethod_id_seq             | sequence | quickbook
 public | Transaction_transaction                        | table    | quickbook
 public | Transaction_transaction_id_seq                 | sequence | quickbook
 public | VehicleDealerManagement_vehicle                | table    | quickbook
 public | VehicleDealerManagement_vehicle_id_seq         | sequence | quickbook
 public | VehicleDealerManagement_vehiclecost            | table    | quickbook
 public | VehicleDealerManagement_vehiclecost_id_seq     | sequence | quickbook
 public | VehicleDealerManagement_vehicledocument        | table    | quickbook
 public | VehicleDealerManagement_vehicledocument_id_seq | sequence | quickbook
 public | VehicleDealerManagement_vehicleimage           | table    | quickbook
 public | VehicleDealerManagement_vehicleimage_id_seq    | sequence | quickbook
 public | VehicleMerk_vehiclemerk                        | table    | quickbook
 public | VehicleMerk_vehiclemerk_id_seq                 | sequence | quickbook
 public | VehicleMerk_vehiclemodel                       | table    | quickbook
 public | VehicleMerk_vehiclemodel_id_seq                | sequence | quickbook
 public | auth_group                                     | table    | quickbook
 public | auth_group_id_seq                              | sequence | quickbook
 public | auth_group_permissions                         | table    | quickbook
 public | auth_group_permissions_id_seq                  | sequence | quickbook
 public | auth_permission                                | table    | quickbook
 public | auth_permission_id_seq                         | sequence | quickbook
 public | auth_user                                      | table    | quickbook
 public | auth_user_groups                               | table    | quickbook
 public | auth_user_groups_id_seq                        | sequence | quickbook
 public | auth_user_id_seq                               | sequence | quickbook
 

 

Comments