Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Multiple Databases - relation does not exist; LINE 1: SELECT COUNT(*) AS "__count" FROM

I am using two databases, one of Mysql other is PostgreSql.

  • Mysql is defined for 'default', PostgreSql is defined for 'location_db'

  • and I have 5 app, let say 'a, b, c, d, location' apps,

  • All apps should run every things on 'default' database except location app,

  • I Just do ' python manage.py runserver ' then every things works fine, and even site works fine, but when I am at admin page and then click ' location ' admin model it runs error * relation "location_locationmodel" does not exist; LINE 1: SELECT COUNT(*) AS "__count" FROM "location_locationmodel" * at the other hands, if I click 'abcd' admin models, it just works fine.

1) this is router.py (I just get the routers code from doc.django site maybe the codes are not for me, because I just guessing my router is not configured properly ? maybe ?)

class LocationRouter(object):

"""
    A router to control all database operations on models in the
    auth application.
    """
    def db_for_read(self, model, **hints):
        """
        Attempts to read auth models go to location_db.
        """
        if model._meta.app_label == 'location':
            return 'location_db'
        return None

    def db_for_write(self, model, **hints):
        """
        Attempts to write auth models go to location_db.
        """
        if model._meta.app_label == 'location':
            return 'location_db'
        return None

    def allow_relation(self, obj1, obj2, **hints):
        """
        Allow relations if a model in the events app is involved.
        """
        if obj1._meta.app_label == 'location' or \
           obj2._meta.app_label == 'location':
           return True
        return None

    def allow_migrate(self, db, app_label, model_name=None, **hints):
        """
        Make sure the auth app only appears in the 'location_db'
        database.
        """
        if app_label == 'location':
            return db == 'location_db'
        return None

2) this is settings.py

DATABASE_ROUTERS = ['location.router.LocationRouter']

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        #'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
        'OPTIONS': {
            'read_default_file': os.path.join(BASE_DIR, 'my.cnf'),
        },
    },
    'location_db': {
        'ENGINE': 'django.contrib.gis.db.backends.postgis',
        'NAME': 'geo',
        'USER': 'john',
        'PASSWORD': 'toor',
        'HOST': 'localhost',
        'PORT': '5432',
    },
}

* My Predictions *

1) If I make another project and only use PostgreSql then every things works fine, So all databases works fine

2) here is when I do 'python manage.py makemigrations or migrate':

Operations to perform:
  Apply all migrations: admin, auth, a, contenttypes, location, b, c, d, registration, sessions, sites
Running migrations:
  No migrations to apply.

* outcomes of migrate I think should not comes with 'location' app or maybe come . This is just prediction I don't really know but when I click ' location ' admin model on the admin page it runs error but it does not run error when I use only one databases, I mean does it searches the table still inside mysql database? could be the router is not configured properly ? *

like image 713
Ozan Honamlioglu Avatar asked Jan 14 '17 17:01

Ozan Honamlioglu


2 Answers

Below code helped me resolve this issue

python manage.py migrate --run-syncdb
like image 185
kiranr Avatar answered Nov 14 '22 21:11

kiranr


Okey Guys Solution Is Not Too much far if you are not suffering by sleepless, In such case you just all specifying everything

  • python manage.py migrate location --database=location_db

and here it is, your queries matches inside postgresql database :) good luck

like image 28
Ozan Honamlioglu Avatar answered Nov 14 '22 21:11

Ozan Honamlioglu