-
Notifications
You must be signed in to change notification settings - Fork 50
Open
Description
For Django projects having multiple DB connections something like this:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'HOST': '127.0.0.1',
'NAME': 'default_db',
'USER': 'postgres',
'PASSWORD': 'xxxxxxx',
'OPTIONS': {'options': '-c search_path=sys,public'},
}
'basf': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'HOST': '127.0.0.1',
'NAME': 'basf_db',
'USER': 'postgres',
'PASSWORD': 'xxxxxxx',
'OPTIONS': {'options': '-c search_path=sys,public'},
},
'bts': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'HOST': '127.0.0.1',
'NAME': 'bts_db',
'USER': 'postgres',
'PASSWORD': 'xxxxxxx',
'OPTIONS': {'options': '-c search_path=sys,public'},
}
}
CSV import (from_csv) doesn't seem to be working. It looks like no matter what ever DB alias I give, it always refer to default DB alias.
eg:
MyModel.objects.using('basf').from_csv('/csv/file/import_path.csv')- Imports the data todefaultDB connection instead ofbasf.MyModel.objects.using('NotExisted').from_csv('/csv/file/import_path.csv')- Still worked and imported todefaultDB Alias instead of throwing error:DB connection doesn't exist.
Though, exporting (to_csv) functionality worked perfectly with DB aliases:
MyModel.objects.using('basf').to_csv('/csv/file/export_path.csv')-- ExportedbasfDB perfectly.MyModel.objects.using('NotExisted').to_csv('/csv/file/export_path.csv')- Raiseddjango.utils.connection.ConnectionDoesNotExistwhich was expected.
Below is my specs:
Ubuntu: 20.04 LTS
Python: 3.8.10
Django==3.2.7
django-postgres-copy==2.7.0
Any quick fix to make CSV import for multiple DB aliases?
I tried something from my end but still didn't work:
from django.db import models
from postgres_copy import CopyQuerySet
class CopyQuerySetWithUsing(CopyQuerySet):
def using(self, alias):
"""Select which database this QuerySet should execute against."""
clone = self._chain()
clone._db = alias
return clone
class CopyManagerWithUsing(models.Manager.from_queryset(CopyQuerySetWithUsing)):
pass
class MyModel(models.Model):
id = models.BigAutoField(primary_key=True)
user_name = models.CharField(max_length=500, blank=True, null=True)
first_name = models.CharField(max_length=500, blank=True, null=True)
last_name = models.CharField(max_length=500, blank=True, null=True)
user_id = models.CharField(max_length=500, blank=True, null=True)
user_email = models.CharField(unique=True, blank=True, null=True)
objects = CopyManagerWithUsing()
class Meta:
db_table = 'my_users'Metadata
Metadata
Assignees
Labels
No labels