coderholic / django-cities

Countries and cities of the world for Django projects
MIT License
920 stars 374 forks source link

./manage.py test fails: psycopg2.ProgrammingError: relation "cities_country" does not exist #181

Closed kiddhustle closed 6 years ago

kiddhustle commented 6 years ago

Checklist

Steps to reproduce

Version

Django==1.11.7 django-cities==0.5.0.5 Python 3.5

  1. Run ./manage.py test in a django project with django-cities installed (via pip install) Please note that the database is being run in a Docker container.

Docker build run command

docker build _docker/postgres --tag wiredapps_postgres
docker run -itd -p 6543:5432 --env-file _docker/postgres/.env --name wiredapps_postgres wiredapps_postgres

Expected behavior

Tests to run

Actual behavior

An error occurs similar to the one found in #129, but migrations run fine.

Creating test database for alias 'default'...
Got an error creating the test database: database "test_wiredapps" already exists

Type 'yes' if you would like to try deleting the test database 'test_wiredapps', or 'no' to cancel: yes 
Destroying old test database for alias 'default'...
Traceback (most recent call last):
  File "/home/boss/.virtualenvs/wiredapps/lib/python3.5/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
psycopg2.ProgrammingError: relation "cities_country" does not exist

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

Traceback (most recent call last):
  File "./manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/home/boss/.virtualenvs/wiredapps/lib/python3.5/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
    utility.execute()
  File "/home/boss/.virtualenvs/wiredapps/lib/python3.5/site-packages/django/core/management/__init__.py", line 356, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/boss/.virtualenvs/wiredapps/lib/python3.5/site-packages/django/core/management/commands/test.py", line 29, in run_from_argv
    super(Command, self).run_from_argv(argv)
  File "/home/boss/.virtualenvs/wiredapps/lib/python3.5/site-packages/django/core/management/base.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/boss/.virtualenvs/wiredapps/lib/python3.5/site-packages/django/core/management/base.py", line 330, in execute
    output = self.handle(*args, **options)
  File "/home/boss/.virtualenvs/wiredapps/lib/python3.5/site-packages/django/core/management/commands/test.py", line 62, in handle
    failures = test_runner.run_tests(test_labels)
  File "/home/boss/.virtualenvs/wiredapps/lib/python3.5/site-packages/django/test/runner.py", line 601, in run_tests
    old_config = self.setup_databases()
  File "/home/boss/.virtualenvs/wiredapps/lib/python3.5/site-packages/django/test/runner.py", line 546, in setup_databases
    self.parallel, **kwargs
  File "/home/boss/.virtualenvs/wiredapps/lib/python3.5/site-packages/django/test/utils.py", line 187, in setup_databases
    serialize=connection.settings_dict.get('TEST', {}).get('SERIALIZE', True),
  File "/home/boss/.virtualenvs/wiredapps/lib/python3.5/site-packages/django/db/backends/base/creation.py", line 69, in create_test_db
    run_syncdb=True,
  File "/home/boss/.virtualenvs/wiredapps/lib/python3.5/site-packages/django/core/management/__init__.py", line 131, in call_command
    return command.execute(*args, **defaults)
  File "/home/boss/.virtualenvs/wiredapps/lib/python3.5/site-packages/django/core/management/base.py", line 330, in execute
    output = self.handle(*args, **options)
  File "/home/boss/.virtualenvs/wiredapps/lib/python3.5/site-packages/django/core/management/commands/migrate.py", line 173, in handle
    self.sync_apps(connection, executor.loader.unmigrated_apps)
  File "/home/boss/.virtualenvs/wiredapps/lib/python3.5/site-packages/django/core/management/commands/migrate.py", line 311, in sync_apps
    self.stdout.write("    Running deferred SQL...\n")
  File "/home/boss/.virtualenvs/wiredapps/lib/python3.5/site-packages/django/db/backends/base/schema.py", line 93, in __exit__
    self.execute(sql)
  File "/home/boss/.virtualenvs/wiredapps/lib/python3.5/site-packages/django/db/backends/base/schema.py", line 120, in execute
    cursor.execute(sql, params)
  File "/home/boss/.virtualenvs/wiredapps/lib/python3.5/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "/home/boss/.virtualenvs/wiredapps/lib/python3.5/site-packages/django/db/utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/home/boss/.virtualenvs/wiredapps/lib/python3.5/site-packages/django/utils/six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "/home/boss/.virtualenvs/wiredapps/lib/python3.5/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: relation "cities_country" does not exist
blag commented 6 years ago

Please give me more information to troubleshoot your issue:

  1. Does the cities_country table actually exist in your test database?
  2. Does django-cities work outside of Docker?
  3. What is the contents of the django_migrations table in your test database?
  4. What is the output of Django's migrations when you run tests?
kiddhustle commented 6 years ago

Hi there, so I've just had a look at the test database:

  1. the cities_country does not exist in my test database The only tables I can see in the test database are:
    List of relations
    Schema |       Name        | Type  |   Owner   
    --------+-------------------+-------+-----------
    public | django_migrations | table | <OWNER>
    public | spatial_ref_sys   | table | <OWNER>
  2. I have just tried django-cities outside of docker and I get the same outcome
  3. On inspection there are actually no rows inside the django_migrations
  4. When runnning tests there is actually no output from the migrations (see the error message above), any idea why this is? I thought the migrations should run automatically when the test database is create?
blag commented 6 years ago

If there are no rows in django_migrations, then the Django migrations did not run. Something is interrupting your tests such that the migrations aren't working. Double check the migration files in your project, or try commenting out apps one by one until migrations work, then adding apps back in one by one to figure out which app is messing up migrations.

As far as I can tell, this isn't an issue with django-cities, this is an issue with your migrations.

kiddhustle commented 6 years ago

So after seeing that migrations were actually being run I started thinking the same and that this was not a problem with django-cities. After deactivating (and re-activating one by one) my installed apps in settings.py I realised that I had an installed app with no migrations. When this was de-activated tests ran as expected.

I think the app migrations came into being in this state after I was squashing migration. Because I am not currently using this app I never go round to migrating it.

I have now removed this app from INSTALLED_APPS and everything works as expected.

Thanks for helping me spot my error and sorry for the confusion!

blag commented 6 years ago

Good to hear you got things working, I'm glad we got to the bottom of your issue! :smile: