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 is None for City model object #195

Open
aliev opened this issue May 10, 2018 · 3 comments
Open

Region is None for City model object #195

aliev opened this issue May 10, 2018 · 3 comments

Comments

@aliev
Copy link

aliev commented May 10, 2018

Checklist

Hello,

Is it possible to make relation between cities and regions? Seems region field is None

Steps to reproduce

In [1]: from cities.models import City

In [2]: City.objects.filter(name='Athens')
Out[2]: <QuerySet [<City: Athens>, <City: Athens>, <City: Athens>, <City: Athens>, <City: Athens>, <City: Athens>]>

In [3]: [(x.country, x.region) for x in City.objects.filter(name='Athens')]
Out[3]: 
[(<Country: Greece>, None),
 (<Country: United States>, None),
 (<Country: United States>, None),
 (<Country: United States>, None),
 (<Country: United States>, None),
 (<Country: United States>, None)]

Regions is exists:

In [1]: from cities.models import Region, City

In [2]: Region.objects.count()
Out[2]: 3918

In [7]: City.objects.filter(region__isnull=False)
Out[7]: <QuerySet []>

Version of cities: django-cities==0.5.0.6

@blag
Copy link
Collaborator

blag commented May 11, 2018

Can you reproduce this after running the import command? And what are the relevant CITIES_ variables in your project's settings.py?

@aliev
Copy link
Author

aliev commented May 11, 2018

@blag I don't have any CITIES_ settings.

import all returns migrations error:

python manage.py cities --import=all
Importing countries: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████| 250/250 [00:00<00:00, 318.16it/s]
Importing country neighbours: 100%|████████████████████████████████████████████████████████████████████████████████████████| 250/250 [00:00<00:00, 481.04it/s]
Building country index: 100%|████████████████████████████████████████████████████████████████████████████████████████████| 250/250 [00:00<00:00, 16723.70it/s]
Importing regions:  82%|███████████████████████████████████████████████████████████████████████████████▋                 | 3254/3958 [00:07<00:01, 449.40it/s]
Traceback (most recent call last):
  File "/Users/ali/Projects/server/env/lib/python3.6/site-packages/django/db/models/query.py", line 503, in update_or_create
    obj = self.select_for_update().get(**lookup)
  File "/Users/ali/Projects/server/env/lib/python3.6/site-packages/django/db/models/query.py", line 403, in get
    self.model._meta.object_name
cities.models.DoesNotExist: Region matching query does not exist.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/ali/Projects/server/env/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
psycopg2.IntegrityError: duplicate key value violates unique constraint "cities_region_country_id_d9299b49_uniq"
DETAIL:  Key (country_id, name)=(7909807, Jonglei) already exists.


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "manage.py", line 29, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/ali/Projects/server/env/lib/python3.6/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
    utility.execute()
  File "/Users/ali/Projects/server/env/lib/python3.6/site-packages/django/core/management/__init__.py", line 365, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/ali/Projects/server/env/lib/python3.6/site-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Users/ali/Projects/server/env/lib/python3.6/site-packages/django/core/management/base.py", line 335, in execute
    output = self.handle(*args, **options)
  File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/contextlib.py", line 52, in inner
    return func(*args, **kwds)
  File "/Users/ali/Projects/server/env/lib/python3.6/site-packages/cities/management/commands/cities.py", line 160, in handle
    func()
  File "/Users/ali/Projects/server/env/lib/python3.6/site-packages/cities/management/commands/cities.py", line 369, in import_region
    region, created = Region.objects.update_or_create(id=region_id, defaults=defaults)
  File "/Users/ali/Projects/server/env/lib/python3.6/site-packages/django/db/models/manager.py", line 82, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/Users/ali/Projects/server/env/lib/python3.6/site-packages/django/db/models/query.py", line 505, in update_or_create
    obj, created = self._create_object_from_params(lookup, params)
  File "/Users/ali/Projects/server/env/lib/python3.6/site-packages/django/db/models/query.py", line 528, in _create_object_from_params
    raise e
  File "/Users/ali/Projects/server/env/lib/python3.6/site-packages/django/db/models/query.py", line 521, in _create_object_from_params
    obj = self.create(**params)
  File "/Users/ali/Projects/server/env/lib/python3.6/site-packages/django/db/models/query.py", line 417, in create
    obj.save(force_insert=True, using=self.db)
  File "/Users/ali/Projects/server/env/lib/python3.6/site-packages/cities/models.py", line 101, in save
    super(Place, self).save(*args, **kwargs)
  File "/Users/ali/Projects/server/env/lib/python3.6/site-packages/cities/models.py", line 72, in save
    super(SlugModel, self).save(*args, **kwargs)
  File "/Users/ali/Projects/server/env/lib/python3.6/site-packages/django/db/models/base.py", line 729, in save
    force_update=force_update, update_fields=update_fields)
  File "/Users/ali/Projects/server/env/lib/python3.6/site-packages/django/db/models/base.py", line 759, in save_base
    updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
  File "/Users/ali/Projects/server/env/lib/python3.6/site-packages/django/db/models/base.py", line 842, in _save_table
    result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
  File "/Users/ali/Projects/server/env/lib/python3.6/site-packages/django/db/models/base.py", line 880, in _do_insert
    using=using, raw=raw)
  File "/Users/ali/Projects/server/env/lib/python3.6/site-packages/django/db/models/manager.py", line 82, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/Users/ali/Projects/server/env/lib/python3.6/site-packages/django/db/models/query.py", line 1125, in _insert
    return query.get_compiler(using=using).execute_sql(return_id)
  File "/Users/ali/Projects/server/env/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1283, in execute_sql
    cursor.execute(sql, params)
  File "/Users/ali/Projects/server/env/lib/python3.6/site-packages/django/db/backends/utils.py", line 100, in execute
    return super().execute(sql, params)
  File "/Users/ali/Projects/server/env/lib/python3.6/site-packages/django/db/backends/utils.py", line 68, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/Users/ali/Projects/server/env/lib/python3.6/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/Users/ali/Projects/server/env/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
  File "/Users/ali/Projects/server/env/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/Users/ali/Projects/server/env/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
django.db.utils.IntegrityError: duplicate key value violates unique constraint "cities_region_country_id_d9299b49_uniq"
DETAIL:  Key (country_id, name)=(7909807, Jonglei) already exists.

also I tried to import only regions. No any errors but it doesn't help.

@blag
Copy link
Collaborator

blag commented May 17, 2018

You already have regions, so I wouldn't expect importing them again to have any effect.

If you re-import cities, it should wire up your cities to the regions that you have.

python manage.py cities --import=city

Let me know if that fixes it or it errors out again.

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

No branches or pull requests

2 participants