Opened 7 years ago

Closed 7 years ago

Last modified 7 years ago

#28515 closed Bug (invalid)

mysql is checking all models also models that are mapped to POSTGIS

Reported by: EDWIN CUBILLOS Owned by: nobody
Component: Database layer (models, ORM) Version: 1.11
Severity: Normal Keywords: postgis, mysql, multidatabase
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by EDWIN CUBILLOS)

version 1.11
models

from django.contrib.gis.db import models

class Ciudad(models.Model):
    nombre = models.CharField(max_length=50, blank=True, null=True, unique=True)
    departamento = models.CharField(max_length=80, blank=True, null=True, unique=True)
    punto = models.PointField(blank=True, null=True)
    polygon = models.PolygonField(blank=True, null=True)
    precio_base = models.TextField(blank=True, null=True)
    precio_yamimo = models.TextField(blank=True, null=True)


class Zona(models.Model):
    nombre = models.CharField(max_length=50, blank=True, null=True, unique=True)
    polygon = models.PolygonField(blank=True, null=True)
    ciudad = models.ForeignKey(Ciudad, blank=True, null=True)
    recargo = models.IntegerField(default=0, blank=True, null=True)

settings

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.humanize',
    'django.contrib.gis',
    'compressor',
    'django_extensions',

    'djcelery',
    'django_crontab',

    'ciudades',
    'api',

]

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'db_1',
        'USER': 'root',
        'PASSWORD': '****',
        'HOST': '127.0.0.1',
        'PORT': '3306',
    },
    'celery': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'db_cel',
        'USER': 'root',
        'PASSWORD': '****',
        'HOST': '127.0.0.1',
        'PORT': '3306',
    },
    'geozone': {
        'ENGINE': 'django.contrib.gis.db.backends.postgis',
        'NAME': 'db_post',
        'USER': 'root',
        # 'PASSWORD': '****',
        'HOST': '127.0.0.1',
        'PORT': '5432',
    }

}

DATABASE_ROUTERS = ['api.router.AutoRouter']

DB_APPS_MAPPING ={
    "ciudades" : "geozone",
    "django"  : "celery",
    "djcelery" : "celery"
 }

Router

from settings import DB_APPS_MAPPING

## class for router app with db
class AutoRouter(object):

    def db_for_read(self, model, **hints):
        # if app_label is not in maps is default db (None
        return DB_APPS_MAPPING.get(model._meta.app_label)

    def db_for_write(self, model, **hints):
        return DB_APPS_MAPPING.get(model._meta.app_label)

    def allow_relation(self, obj1, obj2, **hints):
        app1_db = DB_APPS_MAPPING.get(obj1._meta.app_label)
        app2_db =  DB_APPS_MAPPING.get(obj2._meta.app_label)
        return True if app1_db == app2_db else None

    def allow_migrate(self, db, app_label, model_name=None, **hints):
        return DB_APPS_MAPPING.get(app_label)

ERROR

  File "/Users/c02stf74fvh3/.virtualenvs/Rapigo/lib/python2.7/site-packages/django/core/checks/registry.py", line 81, in run_checks
    new_errors = check(app_configs=app_configs)
  File "/Users/c02stf74fvh3/.virtualenvs/Rapigo/lib/python2.7/site-packages/django/core/checks/model_checks.py", line 30, in check_all_models
    errors.extend(model.check(**kwargs))
  File "/Users/c02stf74fvh3/.virtualenvs/Rapigo/lib/python2.7/site-packages/django/db/models/base.py", line 1283, in check
    errors.extend(cls._check_fields(**kwargs))
  File "/Users/c02stf74fvh3/.virtualenvs/Rapigo/lib/python2.7/site-packages/django/db/models/base.py", line 1358, in _check_fields
    errors.extend(field.check(**kwargs))
  File "/Users/c02stf74fvh3/.virtualenvs/Rapigo/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 219, in check
    errors.extend(self._check_backend_specific_checks(**kwargs))
  File "/Users/c02stf74fvh3/.virtualenvs/Rapigo/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 322, in _check_backend_specific_checks
    return connections[db].validation.check_field(self, **kwargs)
  File "/Users/c02stf74fvh3/.virtualenvs/Rapigo/lib/python2.7/site-packages/django/db/backends/mysql/validation.py", line 49, in check_field
    field_type = field.db_type(self.connection)
  File "/Users/c02stf74fvh3/.virtualenvs/Rapigo/lib/python2.7/site-packages/django/contrib/gis/db/models/fields.py", line 126, in db_type
    return connection.ops.geo_db_type(self)
AttributeError: 'DatabaseOperations' object has no attribute 'geo_db_type'

I have forked the repo from official Django

https://github.com/Cubillosxy/django , and I can pass this issue adding some lines to
lib/python2.7/site-packages/django/db/utils.py

line 294 in class ConnectionRouter

 def allow_migrate(self, db, app_label, **hints):

        try:
            db_app_mapping = settings.DB_APPS_MAPPING.get(app_label, DEFAULT_DB_ALIAS)
            #if database are mapped and database has not app_label
            if db_app_mapping != db:
                return False
        except AttributeError:
            pass

        for router in self.routers:
            try:
                method = router.allow_migrate
            except AttributeError:
                # If the router doesn't have a method, skip to the next one.
                continue

            allow = method(db, app_label, **hints)

            if allow is not None:
                return allow
        return True

I don't know if it was a good solution, or I am doing some wrong in my code.

Change History (7)

comment:1 by Tim Graham, 7 years ago

This looks similar to #28499. Can you confirm that you're using Django 1.11?

comment:2 by EDWIN CUBILLOS, 7 years ago

Description: modified (diff)

yes 1.11
sorry i forgot add version ..

Last edited 7 years ago by EDWIN CUBILLOS (previous) (diff)

comment:3 by Tim Graham, 7 years ago

Resolution: invalid
Status: newclosed

I don't see anything that suggests a bug in Django. Maybe you need to modify your router's allow_migrate() method as you suggested in the description. Please see TicketClosingReasons/UseSupportChannels for ways to get help debugging your code and reopen the ticket if you confirm a bug in Django.

comment:4 by EDWIN CUBILLOS, 7 years ago

I believe that you don't understand my suggest ,
i don't suggest modify my router method
i suggest modify the allow_migrate() method in django/db/utils.py

comment:5 by Tim Graham, 7 years ago

Correct, I don't see the reason why we'd add those lines to Django. settings.DB_APPS_MAPPING is something you've added, it's not an existing setting in Django and I don't see a reason to add it.

comment:6 by Shai Berger, 7 years ago

Hi Edwin,

To expand Tim's comment: Your router's allow_migrate() should return a Boolean. Instead, it returns the app's database-name (from your DB_APPS_MAPPING), which, for every app actually defined there, is truthy.

comment:7 by EDWIN CUBILLOS, 7 years ago

oh , thanks
Shai Berger
that it is the answer,

Note: See TracTickets for help on using tickets.
Back to Top