Skip to content

Multiple DB Connections with DB Alias Does import to correct DB #143

Open
@sauravmahuri2007

Description

@sauravmahuri2007

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:

  1. MyModel.objects.using('basf').from_csv('/csv/file/import_path.csv') - Imports the data to default DB connection instead of basf.
  2. MyModel.objects.using('NotExisted').from_csv('/csv/file/import_path.csv') - Still worked and imported to default DB Alias instead of throwing error: DB connection doesn't exist.

Though, exporting (to_csv) functionality worked perfectly with DB aliases:

  1. MyModel.objects.using('basf').to_csv('/csv/file/export_path.csv') -- Exported basf DB perfectly.
  2. MyModel.objects.using('NotExisted').to_csv('/csv/file/export_path.csv') - Raised django.utils.connection.ConnectionDoesNotExist which 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

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions