Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Region matching query does not exist. #215

Open
jitp opened this issue Apr 14, 2020 · 9 comments
Open

Region matching query does not exist. #215

jitp opened this issue Apr 14, 2020 · 9 comments

Comments

@jitp
Copy link

jitp commented Apr 14, 2020

I installed the package but when I run cities_light command I get that error when script is importing the cities.

Maybe the issue happens when trying to insert a subregion from a non existing region.

def _get_subregion_id(self, country_code2, region_id, subregion_id):
        """
        Simple lazy identity map for (country_code2, region_id,
        subregion_id)->subregion
        """
        country_id = self._get_country_id(country_code2)
        if region_id not in self._region_codes[country_id]:
            self._region_codes[country_id][region_id] = Region.objects.get(
                country_id=country_id, geoname_code=region_id).pk
        if subregion_id not in self._subregion_codes[country_id]:
            self._subregion_codes[country_id][subregion_id] = \
                SubRegion.objects.get(
                country_id=country_id, geoname_code=subregion_id).pk
        return self._subregion_codes[country_id][subregion_id]

On that function on the first if, a query to Region is made and it raises an exception as it doesn't find it but in the outer function calling the above one there is a try except clause but only catches exceptions from non existing Subregions (second if in the above function).

        try:
            subregion_id = self._get_subregion_id(
                items[ICity.countryCode],
                items[ICity.admin1Code],
                items[ICity.admin2Code]
            )
        except SubRegion.DoesNotExist:
            subregion_id = None

So the Region exception goes freely and breaks everything.

@jpic
Copy link
Member

jpic commented Apr 15, 2020 via email

@marianoeramirez
Copy link
Collaborator

@jitp WIll be good if you share a stack trace of the error that you received and more information about your stack, like os, python version and the version of Django.

marianoeramirez added a commit that referenced this issue Apr 17, 2020
@marianoeramirez
Copy link
Collaborator

@jpic on development is fixed this issue and probably another issue.

@jitp
Copy link
Author

jitp commented Apr 18, 2020

Thank you @Bomba1990.

I did the same as you did on my local copy of the source code so I could keep going on using it. After that issue another one came up and it's related to some subregions with the same geoname_code. I solved it like this:

       try:
           subregion_id = self._get_subregion_id(
               items[ICity.countryCode],
               items[ICity.admin1Code],
               items[ICity.admin2Code]
           )
       except (SubRegion.DoesNotExist, Region.DoesNotExist):
           subregion_id = None
       except SubRegion.MultipleObjectsReturned as e:
           subregion_id = \
                   SubRegion.objects.filter(
                       country_id=country_id, region_id=region_id, geoname_code=items[ICity.admin2Code])\
                       .all().first().pk
           self._subregion_codes[country_id][subregion_id] = subregion_id

It is a bit out of design but it solved the issue. I think. I'm using python 3.6.9 with django 1.11 on ubuntu 18.04

@marianoeramirez
Copy link
Collaborator

Yes, I found the same issue @jitp. But I see that the issue was on the _get_subregion_id method, the code for the filter by region is doing by country, I change this to filter by region and work perfectly.

@jitp
Copy link
Author

jitp commented Apr 18, 2020

@Bomba1990 excellent! I just see your commit with the change in _get_subregion_id.

@droptablestar
Copy link

It looks like this issue was never closed and I'm experiencing the same error.

  • Debian GNU/Linux 8.11
  • python 3.6.5
  • Django 2.2.13
  • django-cities-light 3.6.0
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python3.6/site-packages/django/core/management/base.py", line 323, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python3.6/site-packages/django/core/management/base.py", line 364, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python3.6/site-packages/cities_light/management/commands/cities_light.py", line 205, in handle
    self.city_import(items)
  File "/usr/local/lib/python3.6/site-packages/cities_light/management/commands/cities_light.py", line 504, in city_import
    items[ICity.admin2Code]
  File "/usr/local/lib/python3.6/site-packages/cities_light/management/commands/cities_light.py", line 282, in _get_subregion_id
    country_id=country_id, geoname_code=region_id).pk
  File "/usr/local/lib/python3.6/site-packages/django/db/models/manager.py", line 82, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/django/db/models/query.py", line 408, in get
    self.model._meta.object_name
cities_light.models.DoesNotExist: Region matching query does not exist.

@im-khalidbutt
Copy link

I am getting the same error any solution?
Traceback (most recent call last):
File "manage.py", line 30, in
execute_from_command_line(sys.argv)
File "/home/khalid/Desktop/dukaan/dukandaar-web/dukandaar-web/envv/lib/python3.8/site-packages/django/core/management/init.py", line 381, in execute_from_command_line
utility.execute()
File "/home/khalid/Desktop/dukaan/dukandaar-web/dukandaar-web/envv/lib/python3.8/site-packages/django/core/management/init.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/khalid/Desktop/dukaan/dukandaar-web/dukandaar-web/envv/lib/python3.8/site-packages/django/core/management/base.py", line 323, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/khalid/Desktop/dukaan/dukandaar-web/dukandaar-web/envv/lib/python3.8/site-packages/django/core/management/base.py", line 364, in execute
output = self.handle(*args, **options)
File "/home/khalid/Desktop/dukaan/dukandaar-web/dukandaar-web/envv/lib/python3.8/site-packages/cities_light/management/commands/cities_light.py", line 205, in handle
self.city_import(items)
File "/home/khalid/Desktop/dukaan/dukandaar-web/dukandaar-web/envv/lib/python3.8/site-packages/cities_light/management/commands/cities_light.py", line 501, in city_import
subregion_id = self._get_subregion_id(
File "/home/khalid/Desktop/dukaan/dukandaar-web/dukandaar-web/envv/lib/python3.8/site-packages/cities_light/management/commands/cities_light.py", line 281, in _get_subregion_id
self._region_codes[country_id][region_id] = Region.objects.get(
File "/home/khalid/Desktop/dukaan/dukandaar-web/dukandaar-web/envv/lib/python3.8/site-packages/django/db/models/manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/khalid/Desktop/dukaan/dukandaar-web/dukandaar-web/envv/lib/python3.8/site-packages/django/db/models/query.py", line 406, in get
raise self.model.DoesNotExist(
cities_light.models.DoesNotExist: Region matching query does not exist.

@marianoeramirez
Copy link
Collaborator

@im-khalidbutt Please open a new ticket with the error that you have, the versions of Django, and if you can provide more information on your existing database.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants