Seems like a geodjango bug with multiple databases

42 views
Skip to first unread message

Luan Nguyen

unread,
Feb 18, 2015, 9:45:15 AM2/18/15
to django...@googlegroups.com
I'm using geodjango and multiple databases, and experiencing a quite weird situation, I guess it's a kind of bug but not absolutely sure since I'm pretty new to Django.

Here is how to reproduce the problem:
- Set up another database besides default:
DATABASES = {
'default': {
'ENGINE' : 'django.contrib.gis.db.backends.postgis',
'NAME' : 'issue1',
'USER' : 'user',
'PASSWORD' : 'password',
'HOST' : '127.0.0.1',
'OPTIONS' : {
'autocommit' : True,
}
},
'another': {
'ENGINE' : 'django.contrib.gis.db.backends.postgis',
'NAME' : 'issue2',
'USER' : 'user',
'PASSWORD' : 'password',
'HOST' : '127.0.0.1',
'OPTIONS' : {
'autocommit' : True,
}
},
}

And two models:
from django.db import models as default_models
from django.contrib.gis.db import models
# Create your models here.
class Hotel(models.Model):
hotel_name = models.CharField(max_length=255)
objects = models.GeoManager()

class Room(models.Model):
room_num = models.IntegerField()
hotel = models.ForeignKey(Hotel, null=True, blank=True)

Add data into issue2 database (leave issue1 blank), then go into shell:
>>>h = Hotel.objects.using('another').all()[0]
>>> h.id
9
>>>h.room_set.all()[0].id #=> room id 10 links to hotel id 9
10
>>>r = Room.objects.using('another').get(pk=10)
>>>r.hotel
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/Applications/MAMP/htdocs/Rainmaker/vir341/lib/python3.4/site-packages/django/db/models/fields/related.py", line 572, in __get__
rel_obj = qs.get()
File "/Applications/MAMP/htdocs/Rainmaker/vir341/lib/python3.4/site-packages/django/db/models/query.py", line 357, in get
self.model._meta.object_name)
multi.models.DoesNotExist: Hotel matching query does not exist.

The thing is, if I create a hotel record on database issue1 with id of 9 then the last command works, so I guess it tried to look up in default database. This doesn't have any problems if I use default manager for Hotel, so I guess it's a bug?

Collin Anderson

unread,
Feb 19, 2015, 2:11:23 PM2/19/15
to django...@googlegroups.com
Hi,

Interesting. What version of django is this?

Here's something to try:
Room.objects.using('another').select_related('hotel').get(pk=10)

It could very well be an issue with GeoManager / GeoQuerySet.

You also could ask on the geodjango list to see if anyone else has run into that.


Collin

George Silva

unread,
Feb 19, 2015, 2:34:51 PM2/19/15
to django-users
I'm using with success two postgis enabled databases on a single application, without quirks.

I have a read-only database, and a read/write database. No problems, so far.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/64d0f22e-ce10-4626-beef-5f99c63de787%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
George R. C. Silva
SIGMA Consultoria
----------------------------

Jani Tiainen

unread,
Feb 20, 2015, 3:15:01 AM2/20/15
to django...@googlegroups.com
On Wed, 18 Feb 2015 06:45:15 -0800 (PST)
Luan Nguyen <lih...@gmail.com> wrote:

> I'm using geodjango and multiple databases, and experiencing a quite weird
> situation, I guess it's a kind of bug but not absolutely sure since I'm
> pretty new to Django.
>
> Here is how to reproduce the problem:
> - Set up another database besides default:
> DATABASES = {
> 'default': {
> 'ENGINE' : 'django.contrib.gis.db.backends.postgis'
> <http://django.contrib.gis.db.backends.postgis%27/>,
> 'NAME' : 'issue1',
> 'USER' : 'user',
> 'PASSWORD' : 'password',
> 'HOST' : '127.0.0.1',
> 'OPTIONS' : {
> 'autocommit' : True,
> }
> },
> 'another': {
> 'ENGINE' : 'django.contrib.gis.db.backends.postgis'
> <http://django.contrib.gis.db.backends.postgis%27/>,
> 'NAME' : 'issue2',
> 'USER' : 'user',
> 'PASSWORD' : 'password',
> 'HOST' : '127.0.0.1',
> 'OPTIONS' : {
> 'autocommit' : True,
> }
> },
> }
>
> And two models:
> from django.db import models as default_models
> from django.contrib.gis.db import models
> # Create your models here.
> class Hotel(models.Model):
> hotel_name = models.CharField(max_length=255)
> objects = models.GeoManager()
>
> class Room(models.Model):
> room_num = models.IntegerField()
> hotel = models.ForeignKey(Hotel, null=True, blank=True)

You don't have GeoManager on Room model? It's required to be default (first) manager if you do queries that even relate to geo-enabled model.

https://docs.djangoproject.com/en/1.7/ref/contrib/gis/model-api/#geomanager

And there reads: "It should also be noted that GeoManager is required even if the model does not have a geographic field itself, e.g., in the case of a ForeignKey relation to a model with a geographic field."

> Add data into issue2 database (leave issue1 blank), then go into shell:
> >>>h = Hotel.objects.using('another').all()[0]
> >>> h.id
> 9
> >>>h.room_set.all()[0].id <http://h.room_set.all%28%29[0].id/> #=> room id
> 10 links to hotel id 9
> 10
> >>>r = Room.objects.using('another').get(pk=10)
> >>>r.hotel
> Traceback (most recent call last):
> File "<console>", line 1, in <module>
> File "/
> Applications/MAMP/htdocs/Rainmaker/vir341/lib/python3.4/site-packages/django/db/models/fields/related.py
> <http://applications/MAMP/htdocs/Rainmaker/vir341/lib/python3.4/site-packages/django/db/models/fields/related.py>",
> line 572, in __get__
> rel_obj = qs.get()
> File "/
> Applications/MAMP/htdocs/Rainmaker/vir341/lib/python3.4/site-packages/django/db/models/query.py
> <http://applications/MAMP/htdocs/Rainmaker/vir341/lib/python3.4/site-packages/django/db/models/query.py>",
> line 357, in get
> self.model._meta.object_name)
> multi.models.DoesNotExist: Hotel matching query does not exist.
>
> The thing is, if I create a hotel record on database issue1 with id of 9
> then the last command works, so I guess it tried to look up in default
> database. This doesn't have any problems if I use default manager for
> Hotel, so I guess it's a bug?
>
> --
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/daa291e8-bf87-422a-a8fc-b23038e94268%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.


--
Jani Tiainen

Luan Nguyen

unread,
Feb 21, 2015, 11:17:15 PM2/21/15
to django...@googlegroups.com
Hi, sorry for my late response.

Thanks for replying.

I'm using django 1.7.4.

Going with this Room.objects.using('another').select_related('hotel').get(pk=10) is fine, I can then get room.hotel without any problem.

I also followed Jani's advice and changed default manager of Room to GeoManager, but still got that error.

@George: I actually had this issue in one of my app (running django 1.7), but not sure whether it's due to my code or django, so I set up a completely new project as described above (with django 1.7.4), and still get the error. Postgis is enabled on both databases (will get error if it doesn't anyway).

I was trying to trace back the code but don't have enough experience to fully understand the problem. I'll ask geodjango list as Collin suggested to see if someone experienced the same thing.

Luan
Reply all
Reply to author
Forward
0 new messages