#28515: mysql is checking all models also  models that are mapped to POSTGIS
-------------------------------------+-------------------------------------
     Reporter:  EDWIN CUBILLOS       |                    Owner:  nobody
         Type:  Bug                  |                   Status:  new
    Component:  Database layer       |                  Version:  1.11
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:  postgis, mysql,      |             Triage Stage:
  multidatabase                      |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Old description:

> **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.

New description:

 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.

--

Comment (by EDWIN CUBILLOS):

 yes 1.11
 sorry i forget add version ..

-- 
Ticket URL: <https://code.djangoproject.com/ticket/28515#comment:2>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/068.502e5020eed009a460c96ce800972bae%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to